我有一个存储2个日历事件的表,每个行与event_id(int),calendar_id(int - 1/2),start(datetime)和end(datetime)会合。
期望的输出: 来自calendar_id(1)的event_id的新表,没有事件与calendar_id(2)中的事件重叠的开始和结束。
例如,对于以下输入: table example
eventid | calendaid | start | end
101 | 1 | 1/1/10 20:00 | 1/1/10 22:00
102 | 2 | 1/1/10 20:00 | 1/1/10 21:00
103 | 1 | 1/1/10 22:00 | 1/2/10 02:00
104 | 2 | 1/1/10 22:00 | 1/2/10 02:00
105 | 1 | 1/2/10 01:00 | 1/2/10 05:00
106 | 2 | 1/2/10 01:00 | 1/2/10 02:00
107 | 1 | 1/2/10 05:00 | 1/2/10 06:00
108 | 2 | 1/2/10 05:00 | 1/2/10 08:00
109 | 1 | 1/2/10 06:00 | 1/2/10 08:00
110 | 2 | 1/2/10 03:00 | 1/2/10 04:00
输出应如下:
eventid | start | end
101 | 1/1/10 21:00 | 1/1/10 22:00
105 | 1/2/10 02:00 | 1/2/10 03:00
105 | 1/2/10 04:00 | 1/2/10 05:00
有没有办法在MySQL中执行这样的查询?
答案 0 :(得分:0)
我认为你可以用相关的子查询来做到这一点:
num = len(particion)
a = [100] * 2
mini = dist_max
for i in range(num):
pi = particion[i]
for j in range(num):
pj = particion[j]
if (dist[pi,pj] <= mini) & ((incidencia[i] < 2) & (incidencia[j] < 2)):
mini = dist[pi][pj]
a[0] = i
a[1] = j
重叠逻辑假设仅在开始/结束日期时间重叠的事件不重叠。
没有特别有效的方法来实现这种逻辑。 select c.*
from calendars c
where c.calendarid = 1 and
not exists (select 1
from calendars c2
where c2.calendarid = 2 and
c2.start < c.end and
c2.end > c.start
);
上的索引可能有所帮助。