SQL select MAX(COUNT)

后端 未结 5 904
梦如初夏
梦如初夏 2021-01-18 03:54

I\'m trying to select the user who has the MAX microposts count:

SELECT \"name\", count(*) FROM \"users\" 
  INNER JOIN \"microposts\" ON \"microposts\".\"us         


        
5条回答
  •  抹茶落季
    2021-01-18 04:50

    maybe like this:

    SELECT "name", count(*) 
    FROM "users" 
    INNER JOIN "microposts" ON "microposts"."user_id" = "users"."id" 
    GROUP BY users.id
    HAVING COUNT(microposts) = (SELECT COUNT(microposts) 
                             FROM   users
                             GROUP  BY microposts
                             ORDER  BY COUNT(microposts) DESC 
                             LIMIT  1) 
    

    Didn't test it, but it might work

提交回复
热议问题