SQL:查找具有大量注释的用户

时间:2009-12-23 11:03:14

标签: sql mysql

我需要找出发布了最多评论的用户。有两个表1)用户(Id,DisplayName)2)注释(Id,UserId,test)。我使用了以下查询

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 个答案:

答案 0 :(得分: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