MySQL ORDER BY total rows of user in another table

后端 未结 3 857
借酒劲吻你
借酒劲吻你 2021-01-28 15:37

Suppose, I want to show a list of users ordering by the most number of messages they have sent.

I have 2 tables: Users and Messages

I h

3条回答
  •  星月不相逢
    2021-01-28 16:02

    You can sort using an alias:

    SELECT user, COUNT(1) as cnt
    FROM Messages 
    GROUP BY user 
    ORDER BY cnt DESC;
    

    or position:

    SELECT user, COUNT(1) as cnt
    FROM Messages 
    GROUP BY user 
    ORDER BY 2 DESC;
    

提交回复
热议问题