Mysql查找预约系统约会之间的空闲时间(插槽)

时间:2013-09-21 15:05:46

标签: mysql date datetime

我正在尝试在2个日期时间字段之间找到与预订系统的特定持续时间匹配的空闲时间段,但我坚持正确的查询。

我的数据库事件:


+----------+---------------------+---------------------+---------+
| Event_ID | StartTime           | EndTime             | Pers_ID |
+----------+---------------------+---------------------+---------+
|        3 | 2013-09-21 16:00:00 | 2013-09-21 16:30:00 |       6 | 
|       21 | 2013-09-21 09:00:00 | 2013-09-21 09:15:00 |       6 | 
|        5 | 2013-09-21 09:15:00 | 2013-09-21 10:15:00 |       6 | 
|       64 | 2013-09-21 15:00:00 | 2013-09-21 15:45:00 |       6 | 
|       32 | 2013-09-21 10:15:00 | 2013-09-21 11:30:00 |       6 | 
|        6 | 2013-09-21 13:00:00 | 2013-09-21 14:45:00 |       6 | 
+----------+---------------------+---------------------+---------+

结果必须是在2013-09-21 09:00:00和2013-09-21 22:00:00之间Pers_ID = 6的75分钟免费插槽,并且他们必须在EndTime已经在数据库中

已经感谢你的帮助,
E-RASER

1 个答案:

答案 0 :(得分:0)

SELECT AvailStartTime, AvailEndTime
FROM (
    SELECT @lastEndTime as AvailStartTime, StartTime as AvailEndTime, @lastEndTime := EndTime
    FROM (SELECT StartTime, EndTime
          FROM Events
          WHERE Pers_ID = 6
            AND EndTime >= '2013-09-21 09:00'
            AND StartTime < '2013-09-21 22:00'
          ORDER BY StartTime) e
    JOIN (SELECT @lastEndTime := NULL) init) x
WHERE AvailEndTime > DATE_ADD(AvailStartTime, INTERVAL 75 MINUTE)

DEMO