Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SQL:查找注释数最多的用户_Sql_Mysql - Fatal编程技术网

SQL:查找注释数最多的用户

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

我需要找出发表评论最多的用户。有两个表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的用户的显示名称,而不是我想要的名称。我如何解决这个问题?

谢谢。。。我使用的是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