求一高效的sql语句

jom_ch 2010-09-27 11:28:16

现有如下mysql表:

ip(char 15) | day(int 8)
------------------------------------
124.172.230.64 | 20100826
211.141.223.132 | 20100826
113.195.208.187 | 20100826
60.210.19.6 | 20100826
124.165.103.145 | 20100826
222.89.66.191 | 20100826
125.77.213.159 | 20100826
218.6.79.54 | 20100826
124.205.201.34 | 20100827
119.49.119.16 | 20100828
222.85.17.52 | 20100829
... | ...

数据约为2千万

问题:

找出表中ip出现次数大于或等于2的记录,且day不相同

其实就是计算回头用户,比如昨天访问了,今天又访问了,这就算一个回头用户

多谢!







...全文
84 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
ACMAIN_CHM 2010-09-27
  • 打赏
  • 举报
回复
select distinct a.ip
from 如下mysql表 a, 如下mysql表 b
where a.ip=b.ip and a.day>b.day


这个语句应该是效率比较高的一个了,GROUP BY 。。HAVING速度肯定会比较慢。
wwwwb 2010-09-27
  • 打赏
  • 举报
回复
select `ip` from (
select `ip`,`day` from tt group by `ip`,`day`) a group by `ip` having count(*)>=2
zuoxingyu 2010-09-27
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 jom_ch 的回复:]
引用 3 楼 rucypli 的回复:
select distinct ip
from tb A
where exists (select 1 from tb B where A.ip = B.ip and A.day>B.day)


这绝对是一条了不起的的sql语句,1.5千万条数据用了不到3分钟时间
[/Quote]

这条语句将载入史册。
jom_ch 2010-09-27
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 rucypli 的回复:]
select distinct ip
from tb A
where exists (select 1 from tb B where A.ip = B.ip and A.day>B.day)
[/Quote]

这绝对是一条了不起的的sql语句,1.5千万条数据用了不到3分钟时间
rucypli 2010-09-27
  • 打赏
  • 举报
回复
select distinct ip
from tb A
where exists (select 1 from tb B where A.ip = B.ip and A.day>B.day)

56,677

社区成员

发帖
与我相关
我的任务
社区描述
MySQL相关内容讨论专区
社区管理员
  • MySQL
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧