2014-10-31 58 views
0

我有一個事件的表,用戶可以輸入:檢測重疊事件

event_id | title | start    | end 
----------------------------------------------------------------- 
0  | title 1 | 2014-10-29 09:00:00 | 2014-10-29 09:30:00 
1  | title 2 | 2014-10-29 09:15:00 | 2014-10-29 09:45:00 
2  | title 3 | 2014-10-29 10:00:00 | 2014-10-29 10:30:00 
2  | title 3 | 2014-10-29 11:00:00 | 2014-10-29 11:30:00 

我想如果有事件重疊在Cursor返回的附加列。

event_id | title | start    | end     | conflict 
--------------------------------------------------------------------------- 
0  | title 1 | 2014-10-29 09:00:00 | 2014-10-29 09:30:00 | 1 
1  | title 2 | 2014-10-29 09:15:00 | 2014-10-29 09:45:00 | 1 
2  | title 3 | 2014-10-29 10:00:00 | 2014-10-29 10:30:00 | 0 
2  | title 3 | 2014-10-29 11:00:00 | 2014-10-29 11:30:00 | 0 

我試圖找到最好的方法來做到這一點。

回答

0

有衝突,如果存在重疊當前事件的任何其它事件:

SELECT *, 
     EXISTS (SELECT 1 
       FROM events AS other 
       WHERE other.event_id != events.event_id 
       AND other.end > events.start 
       AND other.start < events.end) AS conflict 
FROM events 
+0

謝謝!這正是我需要的 – chis54 2014-10-31 22:18:48