我有一個主表與具有一對多的關係一對多關係的MySQL,順序按三個或更多表
Master
Master_ID, Date, Name, Details
1, 02/10/2011, Bob Smith, example text
Changes
Change_ID, Master_ID, Date, Original, New
1, 1, 05/10/2011, test, test2
2, 1, 06/10/2011, chagge, change
Comments
Comment_ID, Master_ID, Date, Text
1, 1, 05/10/2011, test comment
2, 1, 05/10/2011, more comment
3, 1, 06/10/2011, another
我想參加所有三個表,然後用PHP幾個支持表將所有內容格式化爲一個陣列
SELECT `Master`.*,`Changes`.*,`Comments`.*
FROM Master
JOIN `Changes` USING(Master_ID),
JOIN `Comments` USING(Master_ID)
WHERE `Master`.Master_ID = 1
ORDER BY `Master`.Master_ID,`Changes`.Change_ID,`Comments`.Comment_ID
當我這樣做時,它會按主ID進行排序,然後依次更改ID和Comment ID。但問題是,我希望它根據主ID進行排序,而評論ID根據更改ID進行排序。我嘗試了幾種不同的排序方式,但我無法按照自己的想法做任何幫助,不勝感激。
UPDATE 我已經添加了樣本輸出,如果你會發現CHANGE_ID列未按升序排列,因爲它相對於排序,以CHANGE_ID不Master_ID
Master_ID Date Name Details Change_ID Master_ID Date Original New Comment_ID Master_ID Date Act of Violence
118 19/09/2011 13:13 Bob Smith example text 148 118 12/10/2011 10:42 red reder 309 118 19/09/2011 13:13 test!
118 19/09/2011 13:13 Bob Smith example text 148 118 12/10/2011 10:42 red reder 310 118 19/09/2011 13:14 In Vehicle
118 19/09/2011 13:13 Bob Smith example text 148 118 12/10/2011 10:42 red reder 311 118 19/09/2011 13:14 act of
118 19/09/2011 13:13 Bob Smith example text 148 118 12/10/2011 10:42 red reder 339 118 22/09/2011 13:02 blah blah
118 19/09/2011 13:13 Bob Smith example text 148 118 12/10/2011 10:42 red reder 483 118 12/10/2011 9:24
118 19/09/2011 13:13 Bob Smith example text 148 118 12/10/2011 10:42 red reder 506 118 12/10/2011 10:42
118 19/09/2011 13:13 Bob Smith example text 149 118 12/10/2011 10:42 done none 309 118 19/09/2011 13:13 test!
118 19/09/2011 13:13 Bob Smith example text 149 118 12/10/2011 10:42 done none 310 118 19/09/2011 13:14 In Vehicle
118 19/09/2011 13:13 Bob Smith example text 149 118 12/10/2011 10:42 done none 311 118 19/09/2011 13:14 act of
118 19/09/2011 13:13 Bob Smith example text 149 118 12/10/2011 10:42 done none 339 118 22/09/2011 13:02 blah blah
我寫這個函數將結果排序到一個數組中,該示例函數僅適用於兩個表格,第二個表格具有一對多關係。但是,我有一個更復雜的版本可以處理兩個以上的表格,但問題在於排序。
mysqlResult是從請求mysql_query呼叫關聯數組,parent_key是父表的主鍵,child_key的名稱子表的主鍵的名稱,child_table是子表的名稱,child_fields是在子表
功能cleanJoin($ mysqlResult,$ parent_key,$ child_key,$ child_table,$ child_fields) {所有字段的名稱的關聯數組$ last_parent = 0; $ last_child = 0; $ ch_ctr = 0;
for ($i = 0; $i < count($mysqlResult); $i++)
{
if ($mysqlResult[$i][$child_key] != $last_child)
{
echo "new child!";
$pr_ctr = count($answer[$i]);
foreach ($child_fields as $field => $type)
{
$answer[$pr_ctr][$child_table][$ch_ctr][$field] = $mysqlResult[$i][$field];
unset($mysqlResult[$field]);
}
$ch_ctr++;
}
if ($mysqlResult[$i][$parent_key] != $last_parent)
{
foreach($mysqlResult[$i] as $field => $value)
{
$answer[$i][$field] = $value;
}
}
$last_parent = $mysqlResult[$i][$parent_key];
$last_child = $mysqlResult[$i][$child_key];
}
return $answer;
}
你必須舉一個例子:它現在給出的輸出你不喜歡,B:你想要的輸出。我不知道如何回答這個問題。 – Johan
我已經添加了示例輸出,我想要演示的是第三個主鍵(Comment_ID)未按升序排列 – KopfaufAchseln