Search code examples
mysqlsqlrdbms

Why is my MySQL query not ordering correctly?


In the below query I am trying to order the results by fc.order in ascending 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

However it doesn't seem to be working. Below is a screenshot of results from the above query:

enter image description here

How can I get the results to order by the order column?


Solution

  • ok thanks guys but I got it:

    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`