现有如下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不相同其实就是计算回头用户,比如昨天访问了,今天又访问了,这就算一个回头用户多谢!

解决方案 »

  1.   

    select `ip` from (
    select `ip`,`day` from tt group by `ip`,`day`) a group by `ip` having count(*)>=2
      

  2.   

    select distinct a.ip
    from 如下mysql表 a, 如下mysql表 b
    where a.ip=b.ip and a.day>b.day这个语句应该是效率比较高的一个了,GROUP BY 。。HAVING速度肯定会比较慢。
      

  3.   

    select distinct ip
    from tb A
    where exists (select 1 from tb B where A.ip = B.ip and A.day>B.day)
      

  4.   


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