SnoopD
SnoopD

Reputation: 1

PHP Get the most popular item in table

lets say that my table looks like:

username userid
a1       1
a1       1
a1       1
b2       2
c2       3
d2       3

the most popular username:a1

How do I find the most popular item in table?

Upvotes: 0

Views: 1005

Answers (1)

Amber
Amber

Reputation: 526513

SELECT username, count(*) AS frequency
FROM your_table
GROUP BY username
ORDER BY frequency DESC
LIMIT 1

would yield a single row:

username | frequency
---------+----------
a1         3

Upvotes: 4

Related Questions