2013-02-22 56 views
0

我试图创建特定的查询。 E.g 我有事件表:按日期检查表中有多少条目

id |  start  |  end 
----------------------------------------- 
1 | 10-08-2013 12:00 | 10-08-2013 14:00 
2 | 10-08-2013 12:00 | 10-08-2013 14:00 
3 | 10-08-2013 15:00 | 10-08-2013 16:00 

,我想插入一个新event(start: 13:00, end: 15:30)而在这之前,我想通过查询检查许多事件是如何在同一时间。在这种情况下,结果应为3:因为2个事件处于开始时间,一个处于结束时间。

谢谢。

回答

2

试试这个,

SELECT COUNT(DISTINCT ID) totalCOunt 
FROM tableName 
WHERE new_startDate BETWEEN start AND end 
     OR 
     new_endDate BETWEEN start AND end 

其中new_startDatenew_endDate是事件的新日期。

+0

我觉得@戈登的答案是OP想要什么。如果'new_startDate'为'11:00'并且'new_endDate'为20:00,这将返回'0'。 – 2013-02-23 19:10:18

4

正确的逻辑重叠:

where new_startDate <= end and 
     new_endDate >= start