日期:2014-05-16  浏览次数:20684 次

求简单sql语句
a表 有id、starttime(timestamp时间类型)、endtime(timestamp时间类型)等

b表有id、starttime(timestamp时间类型)、endtime(timestamp时间类型)等

两表id是一样的 时间可能有交集

求sql语句:

将a表记录删除 条件是b表没有任何记录的时间在a表的时间范围内(无交集) 以a.id = b.id匹配

举例:

a表

1 1001 2012-10-10 10:10:10 2012-10-10 10:20:10 

2 1001 2012-10-10 10:30:10 2012-10-10 10:40:10 

b表

1 1001 2012-10-10 10:05:10 2012-10-10 10:11:00 


这样的话 a表第二条记录将删除

PS:效率越高越好 因为数据可能有几万甚至几十万条 而且这个操作很频繁

谢谢..........

------解决方案--------------------
delete from A where exists(select 1 from B where A.id=B.id and (A.starttime>B.endtime or A.endtime<B.starttime));
------解决方案--------------------
delete from A where exists(select 1 from B where A.id=B.id and (A.starttime>B.endtime or A.endtime<B.starttime));


从A中取出来每一条数据和B比较 如果存在 则删除
------解决方案--------------------
delete from A where NOT exists (select 1 from B where A.id=B.id and A.starttime<B.endtime AND A.endtime>B.starttime);
------解决方案--------------------
探讨
delete from A where exists(select 1 from B where A.id=B.id and (A.starttime>B.endtime or A.endtime<B.starttime));


从A中取出来每一条数据和B比较 如果存在 则删除