gpt4 book ai didi

SQL:查找评论数最多的用户

转载 作者:行者123 更新时间:2023-11-29 15:06:18 29 4
gpt4 key购买 nike

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

最佳答案

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

关于SQL:查找评论数最多的用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1952123/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com