MySQL 如何组合 GROUP BY、ORDER BY 和 HAVING

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6734276/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 20:32:17  来源:igfitidea点击:

How to combine GROUP BY, ORDER BY and HAVING

mysqlsql

提问by hmmmm

How do I combine correctly this statement.

我如何正确组合此语句。

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

Let me know

让我知道

回答by JNK

ORDER BYis always last...

ORDER BY永远是最后...

However, you need to pick the fields you ACTUALLY WANTthen select only those and group by them. SELECT *and GROUP BY Emailwill give you RANDOM VALUESfor 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 *并且GROUP BY Email会给你随机值的所有字段,但Email。大多数 RDBMS 甚至不允许您这样做,因为它会产生问题,但 MySQL 是个例外。

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

回答by sudheer

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

使用 Group by、Having By 和 Order by 的步骤...

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

回答by user7488455

Your code should be contain WHILEbefore group byand having:

您的代码应该包含WHILEgroup by和之前having

SELECT Email, COUNT(*)

FROM user_log

WHILE Email IS NOT NULL

GROUP BY Email

HAVING COUNT(*) > 1

ORDER BY UpdateDate DESC