這可能是你的都是經過簡化版本 - 它不包括天,但應該是相當簡單的加:
一個例子表的一些數據:
CREATE TABLE `courses` (
`course_id` int(11) NOT NULL AUTO_INCREMENT,
`time_in` time DEFAULT NULL,
`time_out` time DEFAULT NULL,
PRIMARY KEY (`course_id`)
) ENGINE=InnoDB;
INSERT INTO courses VALUES
(1, '09:00', '10:00'),
(2, '09:30', '10:30'),
(3, '10:00', '11:00'),
(4, '10:30', '11:30'),
(5, '12:00', '13:00');
對於每一行(每個時隙),確定是否有其他行不重疊。請注意,這允許相同的結束和開始時間:
SELECT
c1.course_id,
c1.time_in,
c1.time_out,
c2.course_id,
c2.time_in,
c2.time_out
FROM courses AS c1
JOIN (
SELECT course_id, time_in, time_out FROM courses
) AS c2
ON (c1.time_out > c2.time_in) XOR (c1.time_in < c2.time_out)
ORDER BY c1.course_id, c1.time_in;
+-----------+----------+----------+-----------+----------+----------+
| course_id | time_in | time_out | course_id | time_in | time_out |
+-----------+----------+----------+-----------+----------+----------+
| 1 | 09:00:00 | 10:00:00 | 3 | 10:00:00 | 11:00:00 |
| 1 | 09:00:00 | 10:00:00 | 4 | 10:30:00 | 11:30:00 |
| 1 | 09:00:00 | 10:00:00 | 5 | 12:00:00 | 13:00:00 |
| 2 | 09:30:00 | 10:30:00 | 4 | 10:30:00 | 11:30:00 |
| 2 | 09:30:00 | 10:30:00 | 5 | 12:00:00 | 13:00:00 |
| 3 | 10:00:00 | 11:00:00 | 1 | 09:00:00 | 10:00:00 |
| 3 | 10:00:00 | 11:00:00 | 5 | 12:00:00 | 13:00:00 |
| 4 | 10:30:00 | 11:30:00 | 5 | 12:00:00 | 13:00:00 |
| 4 | 10:30:00 | 11:30:00 | 2 | 09:30:00 | 10:30:00 |
| 4 | 10:30:00 | 11:30:00 | 1 | 09:00:00 | 10:00:00 |
| 5 | 12:00:00 | 13:00:00 | 1 | 09:00:00 | 10:00:00 |
| 5 | 12:00:00 | 13:00:00 | 2 | 09:30:00 | 10:30:00 |
| 5 | 12:00:00 | 13:00:00 | 3 | 10:00:00 | 11:00:00 |
| 5 | 12:00:00 | 13:00:00 | 4 | 10:30:00 | 11:30:00 |
+-----------+----------+----------+-----------+----------+----------+
當您說「超時= 10.3」時,您是否認爲這將是10:30?難道它不會 - 假設每個人都有相同的時間表或多或少 - 在一個單獨的表中有一組標準時隙比較明智,因此時隙1是9-10,時隙2是10-11等等?這會大大簡化您的搜索。 – glenatron 2010-12-07 14:04:43