id  choice_user     choice_ip   varchar       choice_time  bigint(20)
  1   万林            220.250.24.115               1261718881  
  2   万颖            220.250.24.115               1261718885 
  3   小林            220.250.21.1106              1261718901mysql数据库中有张表,像上面类似这样的数据,表中有十几万条这样,用sql语句,怎样写可以  查询同一个ip,5秒内的数据量 删除同一ip,5秒内的数据, 小弟不太懂的写这个,大致分析是id越大的choice_time肯定大于前面一条,打算用后一条的数据减前一条,可以这样写吗,各位帮我一下,不胜感激

解决方案 »

  1.   

    select A.coln - B.coln
    from tb A,tb B
    where a.id+1 = B.id
      

  2.   

     万林 220.250.24.115 1261718881   
     小林 220.250.21.1106 1261718901
     小明 220.250.11.1106 1261718911 万颖 220.250.24.115 1261718885  
     小芳 220.250.11.1106 1261718905 小g 220.250.11.1106 1261718908
    .......可能我没表达清楚,不过,很感谢前面的大哥的回复
    数据有可能是这样的,有几万条这样 ,要是用sql语句删除n秒内同一个ip的数据,怎么写啊
      

  3.   

    select A.coln - B.coln
    from tb A,tb B
    where a.id+1 = B.id and A.choice_ip=B.choice_ip 
    加下条件就可以,好像有点感觉了。呵
      

  4.   


    第一个语句
    delete from t_choice where exists(SELECT A.choice_time-B.choice_time FROM t_choice A,t_choice B where A.choice_id+1=B.choice_id and A.program_id=51 and A.choice_ip=B.choice_ip and A.candidate_id=B.candidate_id and  A.choice_time-B.choice_time<3 )
    第二个语句
    SELECT A.choice_time-B.choice_time FROM t_choice A,t_choice B where A.choice_id+1=B.choice_id and A.program_id=51 and A.choice_ip=B.choice_ip and A.candidate_id=B.candidate_id and  A.choice_time-B.choice_time<3
      //这个语句有内容,我想用delete from这个格式删除第二个sql语句内容,提示#1093 - You can't specify target table 'vote_choice' for update in FROM clause  
      

  5.   

    假设choice_id唯一
    delete A from t_choice A1
    INNER JOIN 
    (SELECT A.*,A.choice_time-B.choice_time FROM t_choice A,t_choice B where A.choice_id+1=B.choice_id and A.program_id=51 and A.choice_ip=B.choice_ip and A.candidate_id=B.candidate_id and  A.choice_time-B.choice_time<3 ) B1
    ON A1.choice_id=B1.choice_id
      

  6.   

    select *
    from table1 a
    where not exists (select 1 from  table1 where choice_ip=a.choice_ip and choice_time > a.choice_time and choice_time<a.choice_time+5)
      

  7.   


    这里delete A from 有这个嘛,还有我去掉后,提示sql语句出错
      

  8.   

    deleete u from table1 u left join (
    select id
    from table1 a
    where not exists (select 1 from  table1 where choice_ip=a.choice_ip and choice_time > a.choice_time and choice_time<a.choice_time+5)
    ) v on u.id=v.id
    where v.id is null
      

  9.   

        表的内容大体是这样,choice_time varchar 内容是时间戳, 像这样的数据有100万条这样,这张表总共有200万条数据这样,我是想抱着学习的心态,就是想弄懂,mysql在查询这样的大数据量时,是不是可以承载的了,希望sql语句带点注解,现在是希望查询出:program_id=7 且candadaite_id=7544的 同一个ip,choice_time在10秒内的数据 (第一条语句)第二个sql语句: 是删除查询出来的第一条语句 ,很感谢前面的几位大侠,希望大家多多指点  choice_id  choice_user  choice_ip  choice_time  candidate_id  program_id  
          ***        林南骏        121.62.39.122  1306161369 7544              7
          ***        卢筱刚        121.62.39.122  1306161372 7544              7
          ***        苏宜欣        121.62.39.123  1306161356 7544              7
          ***        魏慧兴        121.62.39.123  1306161356 7544              7   
          ***        张幼军        121.62.39.124  1306161995 7544              7
          ***        刘建宇        121.62.39.124  1306162007 7544              7
          ***        赵意芳        121.62.39.124  1306162016 7544              7    
          ***        吴怡君        121.62.39.130  1306161357 7544              7          
          ***        郭肇凯        121.62.39.130  1306161358  7544             7      ***        刘建宇1        121.62.39.121  1306162007  7541             8          
          ***        赵意芳2        121.62.39.121  1306162016  7541             8    
          ***        吴怡君3        121.62.39.131  1306161357  7541             8          
          ***        郭肇凯4        121.62.39.131  1306161358  7541             8