MySQL实用语句 GROUP BY ... HAVING ...

时间:2025-08-30 11:42:01来源:互联网

下面小编就为大家分享一篇MySQL实用语句 GROUP BY ... HAVING ...,具有很好的参考价值,希望对大家有所帮助。

问题背景:数据库内有两个表,user,message ,message表中含有useId外键,现在需要统计发的message超过2条的user,并按发的消息条数从高到低排序

 

 

SELECT user.userId, COUNT(msg.messageId) FROM user JOIN message as msg ON user.userId = msg.userId GROUP BY user.userId HAVING COUNT(msg.messageId) > 2 ORDER BY COUNT(msg.messageId) DESC

显示结果

本站部分内容转载自互联网,如果有网站内容侵犯了您的权益,可直接联系我们删除,感谢支持!