MySQL top count({column}) with a limit
- by Josh K
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 so
select ip from table where 1 order by count({distinct ip}) asc limit 5