MySQL调用将两个表与几个相同的行组合在一起

问题描述 投票:0回答:3

我的问题旨在结合两个表的特定SQL调用。

我有两个表,我无法合并为一个表,因为每天从文件中导入一个表。我需要的是什么,我在这里告诉你:

SQL表1摘录:

SQL表2摘录:

在PHP源代码中我需要这个结果:

Array(122, 146, 234, 400)  
Array(John, Dave, Sam, Jana)  
Array(SOME, SOME, Array(ONE, TWO, THREE, FOUR, FIVE), SOME)  
Array(Some Text…, More…, Array(More…, More…, More…, More…, More…), More…)  

“num”行必须与表1一起匹配:

Array(male, male, male, female)  
Array(false, true, false, true)  

我发现的是这个问题:MySQL SELECT AS combine two columns into one

所以我可以使用CONCAT来组合ONE, TWO, THREE, FOUR, FIVE。我可以单独调用每个表并在PHP中构建结果或一起调用两个表。有什么更好的方法?

我不知道的是一个很好的整体概念。请问有人给我一个例子吗?

编辑:

我现在理解语法。这是一个很好的简短解决方案:

mysql > SELECT DISTINCT num, name, GROUP_CONCAT(DISTINCT prefs ORDER BY prefs SEPARATOR ', ') AS array FROM `table1` AS t1 INNER JOIN `table2` AS t2 ON t1.num = t2.num GROUP BY num ORDER BY num
php mysql sql group-concat
3个回答
0
投票

要获取所需的数据,可以使用简单的SELECT...JOIN查询:

select
  T1.num,
  T1,name,
  T2.prefs,
  T2.text,
  T1,gender,
  T1.opt,
from
  Table1 as T1
inner join
  Table2 as T2 on T1.num=T2.num
order by
  T1.num

从这里开始,您可以通过在从DB读取每一行时处理每一行来构建数组(使用PDO和关联提取)

while ($row=$query_result->fetch(PDO::FETCH_ASSOC))
{
  $a1[]=$row['num'];
  $a2[]=$row['name'];
  $a3[$row['num']][]=$row['prefs']; // Store values in a subarray according to num
  $a4[$row['num']][]=$row['prefs']; // Store values in a subarray according to num
  $a5[]=$row['gender'];
  $a6[]=$row['opt'];
}

不幸的是,你最终会在数组1,2,5,6中有多个条目。要解决这个问题,请使用array_unique

$array1=array_unique($a1);
$array2=array_unique($a2);
$array5=array_unique($a5);
$array6=array_unique($a6);

如果您不想使用array_unique,可以使用:

while ($row=$query_result->fetch(PDO::FETCH_ASSOC))
{
  $a1[$row['num']]=$row['num']; // Gets round the array_unique
  $a2[$row['num']]=$row['name'];// Gets round the array_unique
  $a3[$row['num']][]=$row['prefs']; // Store values in a subarray according to num
  $a4[$row['num']][]=$row['prefs']; // Store values in a subarray according to num
  $a5[$row['num']]=$row['gender']; // Gets round the array_unique
  $a6[$row['num']]=$row['opt']; // Gets round the array_unique
}

但是你需要使用array_values来提取值:

$array1=array_values($a1);
$array2=array_values($a2);
$array5=array_values($a5);
$array6=array_values($a6);

要获得结果多维数组,您必须进行更多处理:

$array4=array();
$array5=array();

//Loop through each sub array
foreach ($a4 as $subArray)
{
  //If there is only one element, store that value, otherwise store the sub-array
  $array4[]=(count($subArray)>1) ? array_values($subArray) : array_shift($subArray));
}

//Loop through each sub array
foreach ($a5 as $subArray)
{
  //If there is only one element, store that value, otherwise store the sub-array
  $array5[]=(count($subArray)>1) ? array_values($subArray) : array_shift($subArray));
}

(您可能通过使用变量变量获得闪存,但为了清楚起见,我使用了单独的循环)

一旦获得数组中的数据,就可以用它做你想做的事。如果要从中构建PHP源代码,请使用var_export($arrayName,true)并将其捕获到变量中

这些代码都没有经过实际测试,但希望能指出正确的方向


0
投票

请在MYSQL中使用CONCAT,因为您将直接从一个查询获得结果,而不是通过PHP处理它。


0
投票

试试这个:

$result= mysql_query("select * from table1 t1,table2 t2 where t1.num=t2.num order by t1.prefs asc");

使用函数mysql CONCAT_WS() - > Concatenate With Separator

CONCAT_WS(",",FIELD_NAME,FIELD_NAME)
© www.soinside.com 2019 - 2024. All rights reserved.