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

 

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

 

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

显示结果

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

相关文章:

  • 2021-05-31
  • 2021-11-11
  • 2022-12-23
  • 2022-12-23
  • 2021-09-25
  • 2021-10-26
猜你喜欢
  • 2021-09-25
  • 2021-09-27
  • 2021-10-22
  • 2022-12-23
  • 2022-12-23
  • 2021-07-24
相关资源
相似解决方案