我有两张表,一张是可用时间段,另一张是预订时间段,其中包含开始日期时间和结束日期时间。我想退回空闲时间段。
表1
available_slots
start_datetime |end_datetime
-----------------------------------------
2020-01-14 05:00:00 |2020-01-14 06:00:00
-----------------------------------------
2020-01-14 06:00:00 |2020-01-14 07:00:00
-----------------------------------------
2020-01-14 07:00:00 |2020-01-14 08:00:00
-----------------------------------------
2020-01-14 08:00:00 |2020-01-14 09:00:00
-----------------------------------------
2020-01-14 09:00:00 |2020-01-14 10:00:00
-----------------------------------------
2020-01-14 10:00:00 |2020-01-14 11:00:00
-----------------------------------------
2020-01-14 11:00:00 |2020-01-14 12:00:00
-----------------------------------------
2020-01-14 12:00:00 |2020-01-14 13:00:00
-----------------------------------------
2020-01-14 13:00:00 |2020-01-14 14:00:00
-----------------------------------------
2020-01-14 14:00:00 |2020-01-14 15:00:00
-----------------------------------------
2020-01-14 15:00:00 |2020-01-14 16:00:00
-----------------------------------------
2020-01-14 16:00:00 |2020-01-14 17:00:00
表2
booked_slots
start_datetime |end_datetime
-----------------------------------------
2020-01-14 07:00:00 |2020-01-14 08:00:00
-----------------------------------------
2020-01-14 13:00:00 |2020-01-14 14:15:00
-----------------------------------------
结果应该是
Free slots
start_datetime |end_datetime
-----------------------------------------
2020-01-14 05:00:00 |2020-01-14 06:00:00
-----------------------------------------
2020-01-14 06:00:00 |2020-01-14 07:00:00
-----------------------------------------
2020-01-14 08:00:00 |2020-01-14 09:00:00
-----------------------------------------
2020-01-14 09:00:00 |2020-01-14 10:00:00
-----------------------------------------
2020-01-14 10:00:00 |2020-01-14 11:00:00
-----------------------------------------
2020-01-14 11:00:00 |2020-01-14 12:00:00
-----------------------------------------
2020-01-14 12:00:00 |2020-01-14 13:00:00
-----------------------------------------
2020-01-14 15:00:00 |2020-01-14 16:00:00
-----------------------------------------
2020-01-14 16:00:00 |2020-01-14 17:00:00
最佳答案
SELECT table1.*
FROM table1
WHERE NOT EXISTS (SELECT 1
FROM table2
WHERE table2.start_datetime = table1.start_datetime
AND table2.end_datetime = table1.end_datetime);
关于mysql - 如何从可用时段和预订时段中查找空闲时段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59764380/