【问题标题】:SQL: Finding user with most number of commentsSQL:查找评论数最多的用户
【发布时间】:2009-12-23 19:43:35
【问题描述】:

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

【问题讨论】:

    标签: sql mysql


    【解决方案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
    

    【讨论】:

    • 谢谢...我使用的是 MySql,必须使用 'LIMIT 1' 而不是 'TOP 1'
    猜你喜欢
    • 2012-04-16
    • 1970-01-01
    • 2012-09-05
    • 2012-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-18
    • 1970-01-01
    相关资源
    最近更新 更多