Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Query for multiple count values

SELECT  cm.commenter_id,
        cm.comment,
        m.id,
        (
            SELECT COUNT(*) AS r_count
            FROM comments
            GROUP BY comments.commenter_id
        ) AS count,
        m.display_name
FROM    comments cm
        INNER JOIN members m
            ON cm.commenter_id = m.id

From this query I want to get the display_name for the person with the highest count of comments. Any guidance is appreciated.

like image 834
kabuto178 Avatar asked Oct 22 '22 18:10

kabuto178


1 Answers

SELECT  m.id, m.display_name, COUNT(*) totalComments
FROM    comments cm
        INNER JOIN members m
            ON cm.commenter_id = m.id
GROUP   BY m.id, m.display_name
HAVING  COUNT(*) =
    (
        SELECT  COUNT(*) totalCount
        FROM    Comments
        GROUP   BY  commenter_id
        ORDER   BY totalCount DESC
        LIMIT 1
    )
  • SQLFiddle Demo
  • SQLFiddle Demo (with duplicates)
like image 78
John Woo Avatar answered Oct 27 '22 11:10

John Woo