SQL:查找注释数最多的用户
我需要找出发表评论最多的用户。有两个表1)用户(Id,DisplayName)2)注释(Id,UserId,test)。我使用了以下查询SQL:查找注释数最多的用户,sql,mysql,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) a
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的用户的显示名称,而不是我想要的名称。我如何解决这个问题?谢谢。。。我使用的是MySql,必须使用“限制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