在MySQL中选择缺少的条目

时间:2013-01-01 10:21:12

标签: mysql

我有一个数据库,其中包含每30分钟(+/- ~3秒)保存的数据。大约有2万条记录。现在我希望在没有保存记录的情况下获取所有日期时间:例如,我不想因为它存在于数据库中而希望得到2012-11-22 16:30。但我希望将2012-11-22 16:00作为一个,因为数据库不包含具有该日期的条目。

请记住,秒部分可能会有所不同。通常它只是在分钟,但有时它可能是2012-05-10 10:00:03左右。

我该如何进行此类查询?

3 个答案:

答案 0 :(得分:0)

如果您能够使用存储过程,则可以使用this stored procedure生成系统中最高和最低日期之间的日期时间范围。

如果您无法确定时间戳的最小粒度,那么您可能需要使用秒作为间隔而不是分钟。

针对此表的左连接应显示未保存数据的日期和时间。

答案 1 :(得分:0)

如果您正在寻找差距,更容易查询的是查找下一个晚些时间不在30分钟6秒内的所有时间。

可以在单个查询中执行特定的总时间长度。以下内容将使用特殊表格检查给定范围内的缺失时间,该表格从2010年开始甚至是30分钟(约3.7年):

select t
    from (select date_add('2010-01-01', interval (a+4*b+16*c+64*d+256*e+1024*f+4096*g+16384*h)*30 minute) t from (select 0 a union select 1 union select 2 union select 3) a, (select 0 b union select 1 union select 2 union select 3) b, (select 0 c union select 1 union select 2 union select 3) c, (select 0 d union select 1 union select 2 union select 3) d, (select 0 e union select 1 union select 2 union select 3) e, (select 0 f union select 1 union select 2 union select 3) f, (select 0 g union select 1 union select 2 union select 3) g, (select 0 h union select 1 union select 2 union select 3) h order by t) ad_hoc_times
left join ( your_table, (select -3 t_adj union select -2 union select -1 union select 0 union select 1 union select 2 union select 3) t_adj )
    on your_timestamp=date_add(t, interval t_adj second)
where t between '2010-07-01' and '2012-07-01'
    and your_table.your_timestamp is null;

(您的时间戳字段必须编入索引。)

答案 2 :(得分:0)

我创建了一个表来显示我的存储过程。表创建查询如下所示

CREATE TABLE `testtable1` (
    `id` INT(11) NULL DEFAULT NULL,
    `timecol` DATETIME NULL DEFAULT NULL
)

表包含如下所示的数据

enter image description here

为了满足您的要求,我创建了以下存储过程

DELIMITER $$  
CREATE PROCEDURE proc1(fromtime DATETIME,totime DATETIME)

   BEGIN
      DECLARE a INT Default 1;
      DECLARE temptime DATETIME;
      DECLARE ini,diff,nos int;      
  DECLARE temp1,temp6 datetime;
  drop table if exists mytemptable;
  CREATE TEMPORARY TABLE IF NOT EXISTS mytemptable ( `missing_dates` DATETIME NULL DEFAULT NULL);    

  if(minute(fromtime)>30) then
   set diff=60-(minute(fromtime));       
  else
   set diff=30-(minute(fromtime));       
  end if;
  set temptime=ADDTIME(fromtime,concat('00:',diff,':00'));

  while((unix_timestamp(totime)-unix_timestamp(temptime))>0) DO
  set temp1=SUBTIME(temptime,'00:00:03');     
  set temp6=ADDTIME(temptime,'00:00:03');      

   select count(*) into nos from testtable1 where timecol>=temp1 and timecol<=temp6;
   if(nos=0) then
     insert into mytemptable (missing_dates) values (temptime);
   end if;
  set temptime=ADDTIME(temptime,'00:30:00');
  END WHILE;
 select * from mytemptable;

END $$

要获得所需的结果,只需使用“从时间”和“到时间”调用上述存储过程。例如

call proc1('2013-01-01 14:00:00','2013-01-01 17:00:00')

结果如下:

enter image description here

相关问题