问题背景:数据库内有两个表,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

显示结果

posted on 2012-05-05 20:55  ZimZz  阅读(419)  评论(0编辑  收藏  举报