检测重叠事件

时间:2014-10-31 21:25:25

标签: sqlite overlap

我有一个表,其中包含用户可以输入的事件:

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

如果任何事件重叠,我想在conflict中返回其他列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

我试图找到最佳方法。

1 个答案:

答案 0 :(得分:1)

如果存在与当前事件重叠的任何其他事件,则存在冲突:

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