求简单sql语句

yaozhiyong110 2012-10-18 04:11:07
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:效率越高越好 因为数据可能有几万甚至几十万条 而且这个操作很频繁

谢谢..........
...全文
114 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
yaozhiyong110 2012-10-19
  • 打赏
  • 举报
回复
DELETE FROM A WHERE A.endTime IS NOT NULL AND NOT EXISTS (SELECT 1 FROM B WHERE A.id=B.id AND B.startTime IS NOT NULL AND ((B.endtime IS NOT NULL AND A.starttime<B.endtime AND A.endtime>B.starttime) OR (B.endtime IS NULL AND A.endtime>B.starttime)))

id匹配放前面应该效率高点
yaozhiyong110 2012-10-19
  • 打赏
  • 举报
回复
解决了 最后sql语句:

DELETE FROM A WHERE A.endTime IS NOT NULL AND NOT EXISTS (SELECT 1 FROM B WHERE B.startTime IS NOT NULL AND A.id=B.id AND ((B.endtime IS NOT NULL AND A.starttime<B.endtime AND A.endtime>B.starttime) OR (B.endtime IS NULL AND A.endtime>B.starttime)))


谢谢各位...
liangchengfck1 2012-10-19
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 的回复:]
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比较 如果存在 则删除
[/Quote]0
ACMAIN_CHM 2012-10-18
  • 打赏
  • 举报
回复
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);
yaozhiyong110 2012-10-18
  • 打赏
  • 举报
回复
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中无交集 就删除?
yaozhiyong110 2012-10-18
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 的回复:]
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比较 如果存在 则删除
[/Quote]

这只解释了括号前的意思啊 大哥
rucypli 2012-10-18
  • 打赏
  • 举报
回复
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比较 如果存在 则删除
yaozhiyong110 2012-10-18
  • 打赏
  • 举报
回复
错了 是

引用 1 楼 的回复:
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表

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

2 1001 2012-10-10 10:45:10 2012-10-10 10:51:00

yaozhiyong110 2012-10-18
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]
delete from A where exists(select 1 from B where A.id=B.id and (A.starttime>B.endtime or A.endtime<B.starttime));
[/Quote]

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

2 1001 2012-10-10 10:35:10 2012-10-10 10:41:00


你的语句 这样a的第二条记录会删掉不? 我等下试试

yaozhiyong110 2012-10-18
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]
delete from A where exists(select 1 from B where A.id=B.id and (A.starttime>B.endtime or A.endtime<B.starttime));
[/Quote]


能解释下吗? 特别是括号里的条件不明白.. 谢谢
开启时代 2012-10-18
  • 打赏
  • 举报
回复
delete from A where exists(select 1 from B where A.id=B.id and (A.starttime>B.endtime or A.endtime<B.starttime));

56,687

社区成员

发帖
与我相关
我的任务
社区描述
MySQL相关内容讨论专区
社区管理员
  • MySQL
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧