mysql中count(),group by使用

count()统计表中或数组中记录

count(*)返回检索行的数目,且不论其值中是否包含NULL

count(column_name)返回的是对列中column_name不为NULL的行的统计

例如,查询某活动的某个菜谱的用户评论数:

SELECT COUNT(id) FROM uchome_comment WHERE id=530787 AND idtype='paiid'

 现在想要查询每个用户评论的次数,并且按照评论次数倒序显示:

SELECT COUNT(authorid) AS c, authorid,author FROM uchome_comment WHERE id=530787 AND idtype='paiid' GROUP BY authorid ORDER BY c DESC

当然也可以查询评论次数大于5的:

#在group by 后面使用having子句可以限定分组的条件
SELECT
COUNT(authorid) AS c, authorid,author FROM uchome_comment WHERE id=530787 AND idtype='paiid' GROUP BY authorid HAVING c > 5 ORDER BY c DESC

你可能感兴趣的:(group by)