MySQL-在所有间隔之间插入行日期和时间
我有一个像这样的mysql数据库,
---------------------------------------------------------------------------
| startdate | starttime | enddate | endtime | status |
---------------------------------------------------------------------------
| 2020-03-04 | 04:30:00 | 2020-03-04 | 09:00:00 | running |
| 2020-03-04 | 11:30:00 | 2020-03-04 | 19:30:00 | running |
| 2020-03-05 | 05:00:00 | 2020-03-05 | 11:15:00 | running |
| 2020-03-05 | 12:30:00 | 2020-03-05 | 22:08:00 | running |
---------------------------------------------------------------------------
我想知道是否可能,创建一个php(或类似的东西)脚本来插入日期/时间之间的所有间隔并创建状态为“已停止”的行。
示例:
---------------------------------------------------------------------------
| startdate | starttime | enddate | endtime | status |
---------------------------------------------------------------------------
| 2020-03-04 | 00:00:00 | 2020-03-04 | 04:30:00 | stopped | *created by this script
| 2020-03-04 | 04:30:00 | 2020-03-04 | 09:00:00 | running |
| 2020-03-04 | 09:00:00 | 2020-03-04 | 11:30:00 | stopped | *
| 2020-03-04 | 11:30:00 | 2020-03-04 | 19:30:00 | running |
etc.
---------------------------------------------------------------------------
这可能吗?
对不起我的英语
I have a mysql database like this
---------------------------------------------------------------------------
| startdate | starttime | enddate | endtime | status |
---------------------------------------------------------------------------
| 2020-03-04 | 04:30:00 | 2020-03-04 | 09:00:00 | running |
| 2020-03-04 | 11:30:00 | 2020-03-04 | 19:30:00 | running |
| 2020-03-05 | 05:00:00 | 2020-03-05 | 11:15:00 | running |
| 2020-03-05 | 12:30:00 | 2020-03-05 | 22:08:00 | running |
---------------------------------------------------------------------------
I want know if is possible, create a php(or something like that) script to insert all intervals between date/time and create a row with status "stopped".
Example:
---------------------------------------------------------------------------
| startdate | starttime | enddate | endtime | status |
---------------------------------------------------------------------------
| 2020-03-04 | 00:00:00 | 2020-03-04 | 04:30:00 | stopped | *created by this script
| 2020-03-04 | 04:30:00 | 2020-03-04 | 09:00:00 | running |
| 2020-03-04 | 09:00:00 | 2020-03-04 | 11:30:00 | stopped | *
| 2020-03-04 | 11:30:00 | 2020-03-04 | 19:30:00 | running |
etc.
---------------------------------------------------------------------------
Is this possible?
Sorry my english
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
假设您使用的是 MySQL 8+,您可以使用 LAG() 函数将当前记录的开始日期/时间与上一条记录的结束日期/时间进行比较。当存在差异时,使用这些值来创建缺失的时间跨度:
查询:
此查询将返回缺失的记录,您可以根据需要将其插入表中。
测试数据:
缺失记录:
演示db<>fiddle 此处
Assuming you're using MySQL 8+, you could use the LAG() function to compare the current record's start date/time with the previous record's end date/time. When there's a difference, use those values to create the missing time span:
Query:
This query will return the missing records, which you can insert into your table if desired.
Test Data:
Missing Records:
demo db<>fiddle here