mysql调度碰撞数据

时间:2013-07-15 03:51:43

标签: mysql collision schedule

我有如下的日程安排数据课程,我怎么能检测到它上面有任何数据冲突/冲突数据?在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 |
-
-
-
+-----------+--------+--------+---------+-----------+----------+---------+--------+-------+

抱歉我的英语不好。感谢

2 个答案:

答案 0 :(得分:0)

假设我正确理解你的问题,你是否正在寻找时间范围与其他条目重叠的行?

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

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

答案 1 :(得分:0)

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