我有以下代码在MS sql Server中工作正常:
delete grp from grp left join my_data on grp.id1 = my_data.id1 and grp.id2 = my_data.id2 and grp.id3 = my_data.id3 and grp.id4 = my_data.id4 where my_data.id1 is NULL
基本上,我想删除在grp中可以找到的所有事件,并且在my_data中没有任何等价性.不幸的是,它在Oracle 10g中不起作用.我尝试使用旧的语法左连接(),但它也不工作.喜欢这个:
delete grp from grp,my_data where grp.id1 = my_data.id1 (+) and grp.id2 = my_data.id2 (+) and grp.id3 = my_data.id3 (+) and grp.id4 = my_data.id4 (+) and my_data.id1 is NULL
如果我没有多个密钥,但是我看不到如何使用我的数据,则IN子句将起作用.那么什么办法呢?
表格和数据:
sql> create table grp (id1 number null,id2 number null,id3 number null,id4 number null); Table created. sql> create table my_data (id1 number null,id4 number null); Table created. sql> insert into grp values (1,2,3,4); 1 row created. sql> insert into grp values (10,20,30,40); 1 row created. sql> insert into grp values (1,40); 1 row created. sql> insert into my_data values (1,4); 1 row created. sql> commit; Commit complete.
使用in.注意如果子查询中的ID可以为空,请勿使用.不在null不会返回true.
sql> delete grp where (id1,id2,id3,id4) not in (select id1,id4 from my_data); 2 rows deleted. sql> select * from grp; ID1 ID2 ID3 ID4 ---------- ---------- ---------- ---------- 1 2 3 4
使用存在
sql> rollback; Rollback complete. sql> delete grp where not exists (select * from my_data where grp.id1 = my_data.id1 and grp.id2 = my_data.id2 and grp.id3 = my_data.id3 and grp.id4 = my_data.id4); 2 rows deleted. sql> select * from grp; ID1 ID2 ID3 ID4 ---------- ---------- ---------- ---------- 1 2 3 4 sql>