如何正确组合此声明。
SELECT *, COUNT(*)
FROM user_log
GROUP BY Email
ORDER BY UpdateDate DESC
HAVING COUNT(*) > 1
让我知道
答案 0 :(得分:53)
ORDER BY
永远是最后的......
但是,您需要选择 ACTUALLY WANT 的字段,然后只选择那些并按其分组。 SELECT *
和GROUP BY Email
会为Email
以外的所有字段提供 RANDOM VALUES 。大多数RDBMS甚至不允许你这样做,因为它创建的问题,但MySQL是例外。
SELECT Email, COUNT(*)
FROM user_log
GROUP BY Email
HAVING COUNT(*) > 1
ORDER BY UpdateDate DESC
答案 1 :(得分:-1)
您的代码应在WHILE
和group by
之前包含having
:
SELECT Email, COUNT(*)
FROM user_log
WHILE Email IS NOT NULL
GROUP BY Email
HAVING COUNT(*) > 1
ORDER BY UpdateDate DESC
答案 2 :(得分:-2)
使用分组依据,按顺序排序......
Select Attitude ,count(*) from Person
group by person
HAving PersonAttitude='cool and friendly'
Order by PersonName.