假设我有一张这样的桌子:
+----+----------------+------------+------------+-----------+----------+
| id | time_needed | date_start | date_stop | hour_start| hour_stop |
+----+----------------+------------+------------+-----------+----------+
| 1 | 30 | 2018-08-06 | 2018-08-06 | 08:00:00 | 08:30:00 |
| 2 | 96 | 2018-08-06 | 2018-08-06 | 10:45:00 | 14:21:00 |
| 3 | 20 | NULL | NULL | NULL | NULL |
| 4 | 56 | NULL | NULL | NULL | NULL |
+----+----------------+------------+------------+-----------+----------+我知道什么时候执行操作1和2,但是对于3和4,我只知道执行操作所花费的时间(20和56分钟),当我可以执行操作3时,如何与mysql检查呢?我的意思是,有没有可能检查一下我什么时候有空闲时间,有多长时间?看看表,我可以说我有空闲时间从08:31到10:44,但是有一种方法可以用mysql来检查它吗?
编辑
我想看看早上8点到下午18点的空闲时间。
发布于 2018-08-04 18:40:45
您所要求的不仅仅是mysql的任务。mysql可以根据您的查询提供数据,但是您的编程语言应该尽可能添加调度条目的逻辑。
首先,我将从以下数据库更改开始:
date_start + time_needed来计算它因此,您将得到这样的datatable:
+----+----------------+---------------------+
| id | time_needed | date_start |
+----+----------------+---------------------+
| 1 | 30 | 2018-08-06 08:00:00 |
| 2 | 96 | 2018-08-06 14:21:00 |
+----+----------------+---------------------+现在,您所需要做的就是:将表与其本身连接起来,并确保所有内容都按其应有的方式计算:
-- new Task takes 25 Minutes.
SELECT DISTINCT L.* FROM time_table L LEFT JOIN time_table R
-- compare with every later schedule
on L.date_start < R.date_start
WHERE
-- use only rows that have enough time for the task that should be scheduled
(date_add(L.date_start, INTERVAL L.time_needed + 25 MINUTE ) < R.date_start
-- and only in the future
and date_add(L.date_start, INTERVAL L.time_needed MINUTE) > NOW()
-- due to the join, it might happen that we ignore entries in between.
-- make sure, there is no other task in the calculated timespan!
and not exists
(SELECT id FROM time_table x WHERE
x.date_start > L.date_start AND
x.date_Start < date_add(L.date_start, INTERVAL L.time_needed + 25 MINUTE) )
)
-- and ofc. the last row in the schedule.
OR isnull (R.date_start);这将返回两个数据行,因为25分钟可以安排在每一个其他任务之后。http://sqlfiddle.com/#!9/02020d8/1
14:21 - 08:00 is 6:21和30分钟都是“预定”的。因此,差距是5:51 -所以350分钟任务应该在08:00之后适合-- 351分钟任务不是:
排定时间350分钟:http://sqlfiddle.com/#!9/02020d8/2
需排定的351分钟:http://sqlfiddle.com/#!9/02020d8/3
之后你要做的就是:
取这个小id,然后在它后面插入你的时间表。记住,date_start将是prior_date_start + prior_time_needed。
参见此示例:调度20分钟任务将有3个示例数据选项(为方便起见添加了2列):
https://stackoverflow.com/questions/51688021
复制相似问题