2010-06-09 86 views
0

我有一個查詢的一些困難:MySQL的慢COUNT()

SELECT d.*, 
     (SELECT COUNT(id) FROM downloads WHERE category = d.category) AS count 
FROM downloads d 
GROUP BY d.category 
ORDER BY count DESC 

所以,我試圖讓每個類別總downloadcount但此查詢每次我運行它時掛起。

下載表有+ - 20000行

我在做什麼錯?

回答

4
SELECT category, count(id) as count 
FROM downloads d 
GROUP BY d.category 
ORDER BY count DESC 
+1

這將失敗,如果您正在投射的所有列你會被所有的必須組列也是如此。 – 2010-06-09 20:27:54

+0

哦,男人,我覺得很愚蠢:) THX – Bundy 2010-06-09 20:29:52

+0

糟糕的麪食複製。對不起 – Femaref 2010-06-09 20:32:04

0

我想你會更好,這樣做:(d *)

SELECT category, count(*) as cnt 
    FROM downloads 
GROUP BY category 
ORDER BY cnt desc;