2009-12-23 44 views
0

我需要找出發佈了最多評論數的用戶。有兩個表1)用戶(Id,DisplayName)2)評論(Id,UserId,測試)。我已經使用了以下查詢SQL:查找大多數評論的用戶

Select DisplayName from users INNER JOIN (Select UserId, max(comment_count) as `max_comments from (Select UserId, count(Id) as comment_count from comments group by UserId) as` T1) as T2 ON users.Id=T2.UserId 

但是,這返回給我的Id = 1,而不是我想要的用戶的顯示名稱。我如何解決這個問題?

回答

1
SELECT TOP 1 
U.DisplayName, 
COUNT(C.ID) AS CommentCount 
FROM 
Users AS U 
INNER JOIN Comments AS C ON U.ID = C.UserID 
GROUP BY 
U.DisplayName 
ORDER BY 
COUNT(C.ID) DESC 
+0

謝謝......我使用的是MySql,必須使用'LIMIT 1'而不是'TOP 1' – Stormshadow 2009-12-23 11:24:55