【问题标题】:One to many relationship MySQL, Order By three or more tables一对多关系 MySQL,Order By 三个或更多表
【发布时间】:2011-10-21 08:07:21
【问题描述】:

我有一个主表,其中包含多个支持表,它们具有一对多关系

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是子表所有字段名的关联数组

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;

}

【问题讨论】:

  • 你必须举一个例子:A:现在你不喜欢的输出,B:你想要的输出。我不知道该怎么回答。
  • 我添加了示例输出,我想演示的是第三个主键 (Comment_ID) 是如何不按升序排列的

标签: mysql sorting join one-to-many


【解决方案1】:

我不知道我是否正确理解了您的问题,但也许 GROUP BY 子句的 WITH ROLLUP 修饰符就是您要寻找的答案:http://dev.mysql.com/doc/refman/5.0/en/group-by-modifiers.html

在你的查询中你应该改变

ORDER BY `Master`.Master_ID,`Changes`.Change_ID,`Comments`.Comment_ID

进入

GROUP BY `Master`.Master_ID,`Changes`.Change_ID,`Comments`.Comment_ID WITH ROLLUP

我不确定它是否可以与 JOINS 一起使用,因为文档中的示例都使用单个表,但是签出并没有什么害处。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-05-27
    • 2016-11-22
    • 1970-01-01
    • 2011-08-10
    • 2012-01-30
    • 1970-01-01
    • 2020-04-15
    • 2020-01-24
    相关资源
    最近更新 更多