簡體   English   中英

MySQL調用將兩個表與幾個相同的行組合在一起

[英]MySQL call that combines two Tables with several identical rows

我的問題旨在將兩個表結合在一起的特定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將兩列合並為一個

所以我可以使用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

請在MYSQL中使用CONCAT,因為您將直接從一個查詢中獲得結果,而不是通過PHP處理它。

要獲取所需的數據,可以使用簡單的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

從這里開始,您可以通過處理從數據庫中讀取的每一行來構建數組(將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)並將其捕獲到變量中

這些代碼均未經過實際測試,但希望它將為您指明正確的方向

嘗試這個:

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

使用函數mysql CONCAT_WS()->與分隔符並置

CONCAT_WS(",",FIELD_NAME,FIELD_NAME)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM