嗨,我在mysql DB上有下面的表格。
╔═══════════╦═════════╦════════╦════════════════╗
║ REVIEW_ID ║ USER_ID ║ STATUS ║ DATE_ADDED ║
╠═══════════╬═════════╬════════╬════════════════╣
║ 218 ║ 2 ║ cool ║ 20130121134811 ║
║ 218 ║ 2 ║ cool ║ 20130121134812 ║
║ 218 ║ 2 ║ lame ║ 20130121134813 ║
║ 218 ║ 2 ║ funny ║ 20130121134814 ║
║ 218 ║ 2 ║ funny ║ 20130121134815 ║
║ 218 ║ 2 ║ funny ║ 20130121134816 ║
║ 218 ║ 2 ║ lame ║ 20130121134817 ║
╚═══════════╩═════════╩════════╩════════════════╝当我根据user_id进行查询时,我需要得到每种类型的总状态的结果,如何才能得到结果:
╔════════╦════════════╗
║ STATUS ║ TOTALCOUNT ║
╠════════╬════════════╣
║ cool ║ 2 ║
║ funny ║ 3 ║
║ lame ║ 2 ║
╚════════╩════════════╝谢谢
发布于 2013-01-23 17:32:59
使用聚合函数COUNT(),并根据其status对其进行分组
SELECT status, COUNT(*) totalCount
FROM tableName
GROUP BY status其他(S)
发布于 2013-01-23 17:33:30
SELECT status, count(*)
FROM your_table
GROUP BY status发布于 2013-01-23 17:35:51
SELECT status, count(*)
FROM yourtable
GROUP BY status
;https://stackoverflow.com/questions/14476353
复制相似问题