一对多关系 MySQL,Order By 三个或更多表
我有一个主表,其中有几个具有一对多关系的支持表
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 排序然后是评论 ID。然而问题是,我希望它根据主 ID 进行排序,而注释 ID 则根据更改 ID 进行排序。我尝试了几种不同的排序方式,但我无法让它执行我想要的操作,任何帮助将不胜感激。
更新 我添加了示例输出,如果您会注意到 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;
}
I have a master table with several support tables that have a one to many relationship
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
I want to join all three tables, and then use PHP to format everything into an array
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
When I do this it sorts by the Master ID and then the Change ID and then the Comment ID. However the problem is, that I want it to sort in relation to the Master ID, while the Comment ID gets sorted in relation to the Change ID. I've tried a few different ways of sorting, but I can't get it to do what I want, any help would be appreciated.
UPDATE
I've added sample output, if you'll notice the Change_ID column is not in ascending order, because it's sorted in relation to Change_ID not 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
I've written this function to sort the results into an array, the sample function only works with two tables, with the second table with a one to many relationship. However, I have a more complicated version that works with more than two tables, but the problem is with the sorting.
mysqlResult is an associative array from a mysql_query call, parent_key is the name of the primary key of the parent table, child_key is the name of the primary key of the child table, child_table is the name of the child table, child_fields is an associative array of the names of all the fields in the child table
function 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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不知道我是否正确理解你的问题,但也许 GROUP BY 子句的WITH ROLLUP 修饰符就是你正在寻找的答案: http://dev.mysql.com/doc/refman/5.0/en/group-by-modifiers.html。
在您的查询中,您应该更改
为
“我不确定它是否可以与 JOINS 一起使用”,因为文档中的示例都使用单个表,但是签入没有任何坏处。
I don't know if I understand your question correctly, but maybe the WITH ROLLUP modifier for the GROUP BY clause is the answer you are looking for: http://dev.mysql.com/doc/refman/5.0/en/group-by-modifiers.html.
In your query you should change
into
I am not sure if it will work with JOINS, as the examples in the documenation all use a single table, but there's no harm in checking in out.