How to combine GROUP BY, ORDER BY and HAVING

前端 未结 3 1787
既然无缘
既然无缘 2021-02-01 15:32

How do I combine correctly this statement.

SELECT *, COUNT(*)
FROM user_log
GROUP BY Email
ORDER BY UpdateDate DESC
HAVING COUNT(*) > 1

Let

3条回答
  •  暗喜
    暗喜 (楼主)
    2021-02-01 15:55

    ORDER BY is always last...

    However, you need to pick the fields you ACTUALLY WANT then select only those and group by them. SELECT * and GROUP BY Email will give you RANDOM VALUES for all the fields but Email. Most RDBMS will not even allow you to do this because of the issues it creates, but MySQL is the exception.

    SELECT Email, COUNT(*)
    FROM user_log
    GROUP BY Email
    HAVING COUNT(*) > 1
    ORDER BY UpdateDate DESC
    

提交回复
热议问题