简体   繁体   中英

MySQL - get max value of count

I'm trying to only select Nicknames with the maximum number of posts but I can't seem to be able to get it done... Needless to say the code below doesn't work but this is what I got so far.

SELECT Person.Nickname FROM Posting, Person 
   WHERE Person.Nickname=Posting.Nickname 
   AND count(Posting.PostingID)=(select max(count(Posting.PostingID)))
   GROUP BY Person.Nickname 
   ORDER BY Person.Nickname ASC;

It would be great if someone could help! I'm slowely but surely getting REALLY frustrated and I feel that my problem is something really easy that I'm just overlooking...

Thx in advance for your help!

Edit: This is under the assumption that there is more than one person with the maximum amount of posts. For example: A might have 5 Posts, B might have 4, C might have 5 Posts and so on.

The output should then be: A,C,...

You don't need person table you can get it from Posting

 SELECT Nickname FROM Posting GROUP BY .Nickname having  count(Posting.PostingID) = 
(SELECT count(Posting.PostingID) FROM Posting 
    GROUP BY .Nickname 
       ORDER BY count(Posting.PostingID) desc LIMIT 1);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM