2013-01-23 65 views
4

嗨,我在mysql數據庫上有以下表格。按組計數Mysql count

╔═══════════╦═════════╦════════╦════════════════╗ 
║ 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 ║ 
╚════════╩════════════╝ 

感謝

+0

如果谷歌 「MySQL會以組數」 第一喜噸給你一個非常簡單的解決方案:http://www.w3resource.com/mysql/aggregate-functions-and-grouping/aggregate-functions-and-grouping-count-with-group-by.php – Dan

回答

1
SELECT status, count(*) 
FROM your_table 
GROUP BY status 
+0

+1 @fthiella否則沒有人會投票我們的正確答案;) – bonCodigo

1
SELECT status, count(*) 
FROM yourtable 
GROUP BY status 
; 
+0

謝謝你,以及:) – fthiella

0
SELECT STATUS, COUNT(*) AS TOTALCOUNT 
FROM tableName 
GROUP BY STATUS 
HAVING USER_ID = user_id you need