开发者

How to combine GROUP BY, ORDER BY and HAVING

开发者 https://www.devze.com 2023-03-21 15:56 出处:网络
How do I combine correctly this statement. SELECT *, COUNT(*) FROM user_log GROUP BY Email ORDER BY UpdateDate DESC

How do I combine correctly this statement.

SELECT *, COUNT(*)
FROM user_log
GROUP BY Email
ORDER BY UpdateDate DESC
HA开发者_如何学PythonVING COUNT(*) > 1

Let me know


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


Steps for Using Group by,Having By and Order by...

Select Attitude ,count(*) from Person
group by person
HAving PersonAttitude='cool and friendly'
Order by PersonName.


Your code should be contain WHILE before group by and having :

SELECT Email, COUNT(*)

FROM user_log

WHILE Email IS NOT NULL

GROUP BY Email

HAVING COUNT(*) > 1

ORDER BY UpdateDate DESC
0

精彩评论

暂无评论...
验证码 换一张
取 消