How to find the most common result in a column in my MySQL table

Using PHP and MySQL, I want to query the table of messages that my users made to find the person who posted the most posts.

What will be the correct request for this?

Example table structure:

[id] [UserID]
1 johnnietheblack
2 johnnietheblack
3 dannyrottenegg
4 marywhite
5 marywhite
6 johnnietheblack

I would like to see "johnnietheblack" is the main poster, "marywhite" is in second place, and "dannyrottenegg" has the smallest

+5
source share
3 answers

Sort of:

SELECT COUNT(*) AS `Rows`, UserID
FROM `postings`
GROUP BY UserID
ORDER BY `Rows` DESC
LIMIT 1

, , , , , . "UserID" "postings" .

+13

, ...

SELECT user_id, COUNT(*) FROM postings ORDER BY COUNT(*) GROUP BY user_id LIMIT 1
+1

, (user_id, recipient_user_id), , user_id recipient_user_id:

select user_id, count(*) as posts 
from postings
group by user_id
having count(*) = max(count(*)) ;
0

All Articles