mysql schedule collision data

ε祈祈猫儿з 提交于 2019-12-24 11:27:18

问题


I have schedule data course like below, how i can detect there was any data collision / clashing data on it? in mysql syntax

+-----------+--------+--------+---------+-----------+----------+---------+--------+-------+
| 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 |
-
-
-
+-----------+--------+--------+---------+-----------+----------+---------+--------+-------+

Sorry my bad english. thanks


回答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



回答2:


Assuming I'm understanding your question correctly, you're looking for rows whose time ranges overlap with other entries?

If so, you can compare your times like this:

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

EDIT: As @Barmar has appropriately pointed out, this will produce results that will conflict with themselves. Do you have a primary key on this table? Add it (or your unique identifier) to your JOIN or WHERE criteria:

WHERE T.PrimaryKey <> T2.PrimaryKey


来源:https://stackoverflow.com/questions/17646503/mysql-schedule-collision-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!