为什么我的MySQL查询没有正确排序?

时间:2016-06-25 05:07:46

标签: mysql sql rdbms

在下面的查询中,我尝试按fc.order按升序排序结果:

SELECT *, count(*) AS threads_count FROM (
    SELECT fc.uuid AS category_uuid, fc.name, fc.description, fc.order, fc.icon,
           u.uuid AS user_uuid, u.username, u.avatar
    FROM forum_categories AS fc
    INNER JOIN forum_threads AS ft ON fc.id = ft.forum_category_id
    INNER JOIN users as u ON ft.created_by = u.id
    ORDER BY fc.order ASC, ft.created_at DESC
) AS tmp_table GROUP BY category_uuid

然而它似乎并没有起作用。以下是上述查询结果的屏幕截图:

enter image description here

如何通过order列获取结果?

2 个答案:

答案 0 :(得分:1)

好的,谢谢你们,但我明白了:

SELECT * FROM (
    SELECT *, count(*) AS threads_count FROM (
        SELECT fc.uuid AS category_uuid, fc.name, fc.description, fc.order, fc.icon,
               u.uuid AS user_uuid, u.username, u.avatar
        FROM forum_categories AS fc
        INNER JOIN forum_threads AS ft ON fc.id = ft.forum_category_id
        INNER JOIN users as u ON ft.created_by = u.id
        ORDER BY ft.created_at DESC
    ) AS t1 GROUP BY category_uuid
) as t2 ORDER BY `order`

答案 1 :(得分:0)

SELECT t1.*, count(t1.*) AS threads_count FROM 
 (
        SELECT fc.uuid AS category_uuid, fc.name, fc.description, fc.order, fc.icon,
               u.uuid AS user_uuid, u.username, u.avatar
        FROM forum_categories AS fc
        INNER JOIN forum_threads AS ft ON fc.id = ft.forum_category_id
        INNER JOIN users as u ON ft.created_by = u.id    
 ) AS tmp_table t1 
GROUP BY category_uuid
ORDER BY t1.order ASC, t1.created_at DESC