我希望在特定日期获得时间冲突
我的桌子
| starttime | endtime | day | subject |
------------------------------------------
| 09:00:00 | 11:00:00 | M | math |
-----------------------------------------
| 09:00:00 | 11:00:00 | M | science |
-----------------------------------------
| 06:00:00 | 09:00:00 | M | History |
-----------------------------------------
| 06:00:00 | 09:00:00 | T | P.E. |
-----------------------------------------
| 10:00:00 | 12:00:00 | M | English |
-----------------------------------------
| starttime | endtime | day | subject |
------------------------------------------
| 09:00:00 | 11:00:00 | M | math |
-----------------------------------------
| 09:00:00 | 11:00:00 | M | science |
-----------------------------------------
| 10:00:00 | 12:00:00 | M | English |
-----------------------------------------
答案 0 :(得分:0)
对我来说唯一有意义的问题的唯一解释是,您希望识别时间范围与一个或多个其他时间范围重叠(“冲突”)的记录。如果是这样,我们可以在重叠条件下进行自连接,然后保留那些有重叠的范围。
SELECT DISTINCT
t1.starttime,
t1.endtime,
t1.day,
t1.subject
FROM yourTable t1
LEFT JOIN yourTable t2
ON
(t1.starttime > t2.starttime AND t1.starttime < t2.endtime AND t1.day = t2.day) OR
(t1.endtime > t2.starttime AND t1.endtime < t2.endtime AND t1.day = t2.day) OR
(t1.starttime = t2.starttime AND t1.endtime = t2.endtime AND t1.day = t2.day AND
t1.subject <> t2.subject)
WHERE
t2.starttime IS NOT NULL;
<强>输出:强>
starttime endtime day subject
1 07:00:00 10:00:00 T History
2 07:00:00 10:00:00 T P.E.
在这里演示: