比如我现在有一个表,字段如下
id ip dateid是自动编号
ip是访问本站的ip地址(可重复)
date是访问时间请问如何得出以下结果
访问最多的ip按访问次数排序
自动编号 ip地址 访问次数
1 192.168.1.3 8
2 192.168.1.8 5
3 192.168.1.5 4
4 192.168.1.2 3
5 192.168.1.9 1
id ip dateid是自动编号
ip是访问本站的ip地址(可重复)
date是访问时间请问如何得出以下结果
访问最多的ip按访问次数排序
自动编号 ip地址 访问次数
1 192.168.1.3 8
2 192.168.1.8 5
3 192.168.1.5 4
4 192.168.1.2 3
5 192.168.1.9 1
order by times desc
你编号查询出来就不是连续的了!没必要再查询出来了吧!
select distinct ip,count(ip) as times from tb
group by ip
order by times desc
order by times desc
对头
select id as 自动编号,ip as ip地址,count(ip) as 访问次数 from t group by ip order by 访问次数
肯定要分组的,不然count或sum都是全部计算了,
csdn高人好多,学习ing
(SELECT ip,COUNT(*) cnt FROM tb GROUP BY ip) b
WHERE ip!=a.ip AND cnt>=a.cnt
) 排名
ip ip地址,cnt 访问次数
FROM
(SELECT ip,COUNT(*) cnt FROM tb GROUP BY ip) a
(SELECT ip,COUNT(*) cnt FROM tb GROUP BY ip) b
WHERE ip!=a.ip AND cnt>=a.cnt
) 排名,
ip ip地址,cnt 访问次数
FROM
(SELECT ip,COUNT(*) cnt FROM tb GROUP BY ip) a另外,以上语句是不考滤某几个ip的count数相同的情况.
若考滤这个情况,再按id比较即可,不写了.
select id as 自动编号,ip as ip地址,count(ip) as 访问次数 from t group by ip order by 访问次数