如何检查一天中每分钟是否在MySQL数据库中有一行?

时间:2014-11-06 13:48:05

标签: php mysql bigdata

我目前有

SELECT *, COUNT(*) as `numrows`
FROM `data`
WHERE `datetime`
    BETWEEN '2014-11-04 00:00:00' AND '2014-11-04 23:59:59'
AND `project_id` = 1
GROUP BY
 DATE(`datetime`),
 HOUR(`datetime`),
 MINUTE(`datetime`)

现在,这将返回所有行,其中一些有重复,因此numrows将为2.但是,我想验证每一分钟是否有一行,所以我可以验证测量是否仍然正确进入。

现在,我已经考虑过循环结果,将每个返回的日期时间添加到数组中:

while($s = $select->fetch_assoc()) {
 $availableTimes[] = $s['datetime'];
}

然后,在一个充满了当天会议记录的数组中循环,但我认为这是非常密集的,特别是当你有500,1000,5000甚至30000个ID时。最好的办法是什么?

4 个答案:

答案 0 :(得分:1)

如果您在当天每分钟只获得一行(使用分组依据),您可以使用您获得的行数。 你每天应该有24 * 60的结果(根据我的理解,你只需要......?)

答案 1 :(得分:0)

您可以像这样创建一个包含小时和分钟的临时表。您可以使用DISTINCT删除任何重复项。

SELECT DISTINCT HOUR(`datetime`) AS HOUR, MINUTE(`datetime`) AS MINUTE
FROM `data`
WHERE `datetime`
    BETWEEN '2014-11-04 00:00:00' AND '2014-11-04 23:59:59'
AND `project_id` = 1

然后你可以通过在外面添加一个选择计数(*)来计算临时表中的行数

SELECT COUNT(*) FROM (
SELECT DISTINCT HOUR(`datetime`) AS HOUR, MINUTE(`datetime`) AS MINUTE
FROM `data`
WHERE `datetime`
    BETWEEN '2014-11-04 00:00:00' AND '2014-11-04 23:59:59'
AND `project_id` = 1) AS TEMP

如果所有小时和分钟都有行,则结果应为1440(24 * 60)。

答案 2 :(得分:0)

您可以使用这样的过程为每分钟创建一个表。

CREATE PROCEDURE `make_intervals`(startdate timestamp, enddate timestamp, intval integer, unitval varchar(12))
BEGIN


   declare thisDate timestamp;
   declare nextDate timestamp;
   set thisDate = startdate;




   drop temporary table if exists time_intervals;
   create temporary table if not exists time_intervals
      (
      interval_start timestamp,
      interval_end timestamp
      );




   repeat
      select
         case unitval
            when 'MICROSECOND' then timestampadd(MICROSECOND, intval, thisDate)
            when 'SECOND'      then timestampadd(SECOND, intval, thisDate)
            when 'MINUTE'      then timestampadd(MINUTE, intval, thisDate)
            when 'HOUR'        then timestampadd(HOUR, intval, thisDate)
            when 'DAY'         then timestampadd(DAY, intval, thisDate)
            when 'WEEK'        then timestampadd(WEEK, intval, thisDate)
            when 'MONTH'       then timestampadd(MONTH, intval, thisDate)
            when 'QUARTER'     then timestampadd(QUARTER, intval, thisDate)
            when 'YEAR'        then timestampadd(YEAR, intval, thisDate)
         end into nextDate;

      insert into time_intervals select thisDate, timestampadd(MICROSECOND, -1, nextDate);
      set thisDate = nextDate;
   until thisDate > enddate
   end repeat;
end


call make_intervals('2014-11-04 00:00:00' ,'2014-11-04 23:59:59',1,'MINUTE');

比将左边的表连接到timer_intervals表并查找缺失

答案 3 :(得分:0)

一种方法是生成一系列临时行,每天您感兴趣的一分钟(1440行)。然后你LEFT OUTER JOIN你的数据到这个,并计算在数据表上找到的数据时间的行数。

SELECT aDateTime, COUNT(data.datetime)
FROM
(
    SELECT DATE_ADD('2014-11-04 00:00:00', INTERVAL a.Mnt + b.Mnt * 10 + c.Mnt * 100 + d.Mnt * 1000 MINUTE) AS aDateTime
    FROM (SELECT 0 AS Mnt UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) a
    CROSS JOIN (SELECT 0 AS Mnt UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) b
    CROSS JOIN (SELECT 0 AS Mnt UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) c
    CROSS JOIN (SELECT 0 AS Mnt UNION SELECT 1) d
    WHERE (a.Mnt + b.Mnt * 10 + c.Mnt * 100 + d.Mnt * 1000) < 1440
) sub0
LEFT OUTER JOIN `data` 
ON sub0.aDateTime = data.datetime
AND data.project_id = 1
GROUP BY aDateTime