MySQL的时间表碰撞数据(mysql schedule collision data)

2019-10-18 08:47发布

我有时间表数据当然像下面,我怎么可以检测到有任何数据冲突/上发生冲突的数据吗? 在MySQL语法

+-----------+--------+--------+---------+-----------+----------+---------+--------+-------+
| IDCourse  | Day    | Class  | Room    | IDTeacher | Capacity | Residue | Begin  | End   |
+-----------+--------+--------+---------+-----------+----------+---------+--------+-------+
| MI09BB11  | Monday | A      | A.1.4   | RA        | 40       | 1       | 08:00  | 10:00 |
| MI09BB12  | Monday | A      | A.1.4   | RA        | 40       | 1       | 08:30  | 10:30 | <-- clash 
| MI09BB51  | Monday | A      | A.1.4   | RA        | 40       | 1       | 11:00  | 13:00 |
-
-
-
+-----------+--------+--------+---------+-----------+----------+---------+--------+-------+

对不起我的英文不好。 谢谢

Answer 1:

SELECT s1.IDCourse, s2.IDCourse, s1.Day
FROM schedule s1
JOIN schedule s2
ON s1.Day = s2.Day AND s1.Room = s2.Room AND s1.IDCourse != s2.IDCourse
WHERE s1.Begin BETWEEN s2.Begin AND s2.End


Answer 2:

假设我正确理解你的问题,你要找的行,其时间范围内与其他项目重叠?

如果是这样,你可以比较你的时间是这样的:

SELECT T2.IDCourse, T2.Begin, T2.End
FROM YourTable T
    INNER JOIN YourTable T2 ON
            T.Day = T2.Day AND
            T.Begin <= T2.End AND 
            T.End >= T2.Begin

编辑:由于@Barmar已适当地指出,这将产生的结果将与自己发生冲突。 你对这个表的主键? 它(或您的唯一标识符)添加到您的JOINWHERE条件:

WHERE T.PrimaryKey <> T2.PrimaryKey


文章来源: mysql schedule collision data