2012-08-31 94 views
0

我有一個名爲ratings的表和一個名爲item_id的列。我只是想將每個單獨的項目組合在一起並對它們進行計數。在一列中計算特定項目

例如:

如果有1000行,和item_id 51已經被評爲20次,item_id 14已經被評爲7倍,item_id 33已被評爲2次,我只是希望它被分組在DESC這樣的:

ITEM_ID 51 ITEM_ID 7 ITEM_ID 33

我試圖按照類似this question,但不能讓它開始工作。

回答

3
SELECT item_id, COUNT(*) AS count 
FROM ratings 
GROUP BY item_id 
ORDER BY count DESC; 
+0

完美的感謝! – KickingLettuce

1
SELECT count(*) AS counter, item_id 
FROM ratings 
GROUP BY item_id 
ORDER BY counter DESC 
1
SELECT * 
FROM (
    SELECT item_id, COUNT(rating) AS rating 
    FROM ratings GROUP BY item_id 
) a 
GROUP BY a.rating DESC