我基本上每分钟都会将数据记录到 SQL 数据库,但我希望能够检查是否有任何条目丢失(即:查看是否有任何大于一分钟的差异)。
以此为例:
5
4
2
1
我想显示 4 和 2 所在的行,以便我知道之间缺少一些内容。实际数据的格式如下。
date_time
2010-09-12 10:45:00
2010-09-12 10:44:00
等等。
我发现了如何使用 EXTRACT(MINUTE FROM date_time) 提取分钟值,但是我很迷失如何对整个表进行比较。
任何帮助都会很棒。
谢谢
I'm basically logging data to an SQL database every minute, but I want to be able to check to see if there are any entries missing (ie: see if there are any differences greater than a minute).
Take this for example:
5
4
2
1
I would want to display the rows where the 4 and 2 exist so that I know there is something missing between. The actual data is in the following format.
date_time
2010-09-12 10:45:00
2010-09-12 10:44:00
etc..
I have discovered how to extract the minute value using EXTRACT(MINUTE FROM date_time) however I'm quite lost as to how to do a comparison over the whole table.
Any help would be awesome.
Thanks
发布评论
评论(1)
MySQL 没有递归功能,因此您只能使用 NUMBERS 表技巧 -
创建一个仅保存递增数字的表 - 使用 auto_increment 很容易做到:
使用以下方式填充表:
...您需要的任意数量的值。
使用DATE_ADD< /a> 构建日期列表,根据
NUMBERS.id
值增加天数。将“2010-06-06”和“2010-06-14”替换为各自的开始日期和结束日期(但使用相同的格式“YYYY-MM-DD HH:MM:SS”)-<前><代码>选择 x.*
FROM (SELECT DATE_ADD('2010-06-06', INTERVAL n.id - 1 分钟)
从数字 n
WHERE DATE_ADD('2010-06-06', INTERVAL n.id -1 分钟) <= '2010-06-14' ) x
LEFT JOIN to基于时间部分的数据表:
有关 DATE_FORMAT 函数。
MySQL doesn't have recursive functionality, so you're left with using the NUMBERS table trick -
Create a table that only holds incrementing numbers - easy to do using an auto_increment:
Populate the table using:
...for as many values as you need.
Use DATE_ADD to construct a list of dates, increasing the days based on the
NUMBERS.id
value. Replace "2010-06-06" and "2010-06-14" with your respective start and end dates (but use the same format, "YYYY-MM-DD HH:MM:SS") -LEFT JOIN onto your table of data based on the time portion:
See the documentation for more info on the DATE_FORMAT function.