开发者

MySQL top count({column}) with a limit

开发者 https://www.devze.com 2022-12-29 01:52 出处:网络
I have a table with an ip address column. I would like to find the top five addresses which are listed.

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:

  1. Select all distinct ip addresses
  2. Loop through them all saying count(id) where IP='{ip}' and storing the count
  3. 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 :)

0

精彩评论

暂无评论...
验证码 换一张
取 消