如何结合GROUP BY,ORDER BY和HAVING [英] How to combine GROUP BY, ORDER BY and HAVING

查看:102
本文介绍了如何结合GROUP BY,ORDER BY和HAVING的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何正确组合此语句.

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

让我知道

推荐答案

ORDER BY永远是最后一个...

ORDER BY is always last...

但是,您需要选择要完全想要的字段,然后仅选择这些字段并按其分组.对于Email以外的所有字段,SELECT *GROUP BY Email将为您提供 RANDOM VALUES .大多数RDBMS甚至会由于其创建的问题而不允许您执行此操作,但是MySQL是例外.

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

这篇关于如何结合GROUP BY,ORDER BY和HAVING的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆