MySQL top count({column}) with a limit
I have a table with an ip address column. I would like to find the top five addresses which are listed.
Right now I'm planning it out the following:
- Select all distinct ip addresses
- Loop through them all saying
count(id) where IP='{ip}'
and storing the count - List the top five counts.
Downsides include what if I have 500 ip addresses. That's 500 queries I have to run to figure out what are the top five.
I'd like to build a query like s开发者_运维百科o
select ip from table where 1 order by count({distinct ip}) asc limit 5
select ip, count(*)
from table
group by ip
order by count(*) desc limit 5
select IP, count(IP) as IPCount from TABLE
group by IP
order by IPCount DESC
Limit 5
There is :)
精彩评论