6

嗨,我在 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 ║
╚════════╩════════════╝

谢谢

4

4 回答 4

13

使用COUNT()which 是一个聚合函数,并根据它们对它们进行分组status

SELECT  status, COUNT(*) totalCount
FROM    tableName
GROUP   BY status

其他)

于 2013-01-23T09:32:59.410 回答
1
SELECT status, count(*)
FROM your_table
GROUP BY status
于 2013-01-23T09:33:30.387 回答
1
SELECT status, count(*)
FROM yourtable
GROUP BY status
;
于 2013-01-23T09:35:51.343 回答
0
SELECT  STATUS, COUNT(*) AS TOTALCOUNT
FROM    tableName
GROUP   BY STATUS
HAVING   USER_ID = user_id you need
于 2013-01-23T10:44:29.910 回答