赞成: select col1,col2 from a where not exists (select col1,col2 from b where a.col1=b.col1 and a.col2=b.col2);
还是zlqs(啊啊啊) 同学看的仔细,赞! 表A中有两条444 8,而表B中有一条,用a.col1=b.col1 and a.col2=b.col2 根本得不出需要的444 8。 只能用minus,个人意见!
minus一样没有用,一样不能解决两条444 8的问题
SQL> select * from a;ID NAME ---------- ---------- 123 1 123 2 123 3 444 8 444 8 247 6 852 5已选择7行。SQL> select * from b;ID NAME ---------- ---------- 123 1 123 3 444 8 852 5SQL> select * from a 2 minus 3 select * from b;ID NAME ---------- ---------- 123 2 247 6SQL> select row_number() over(partition by a.id,a.name order by a.id,a.name) rn,a.id,a.name from a 2 minus 3 select row_number() over(partition by b.id,b.name order by b.id,b.name) rn,b.id,b.name from b; RN ID NAME ---------- ---------- ---------- 1 123 2 1 247 6 2 444 8 这个上面的言论不负责任,没有测试。 现经过测试,发现minus确实会把444 8 也过滤掉,不好意思。 下面的那个sql是根据两条数据是一致的,那么就人为给他个 顺序号,这样就变成不一样的记录了,自然可以分辨出来。
SQL> select * from a; A1 A2 ----------------------------- ---------- 1 a 2 ba 3 ca 4 daSQL> select * from b; B1 B2 ----------------------------- ---------- 1 a 2 bba 3 ca 5 dda 6 Eda 1 a6 rows selectedSQL> SQL> select * from ( 2 select b1,b2,row_number() over (partition by b1 order by b1) from b 3 minus 4 select a1,a2,row_number() over (partition by a1 order by a1) from a); B1 B2 ROW_NUMBER()OVER(PARTITIONBYB1 ---------- ---------- ------------------------------ 1 a 2 2 bba 1 5 dda 1 6 Eda 1SQL> 这个倒是可以实现,不过效率可能不是很好
select col1,col2 from a where not exists (select col1,col2 from b where a.col1=b.col1 and a.col2=b.col2);
minus的效率很差。
子查询如果数据量大的话效果更差
select col1,col2 from a where not exists (select col1,col2 from b where a.col1=b.col1 and a.col2=b.col2);
select col1,col2 from a where not exists (select col1,col2 from b where a.col1=b.col1 and a.col2=b.col2);
的结果可能是
123 2
444 8
247 6
852 5
吗?
如果子集很少的话,可以使用not in,否则not in是最慢的。
select col1,col2 from a where not exists (select col1,col2 from b where a.col1=b.col1 and a.col2=b.col2);
表A中有两条444 8,而表B中有一条,用a.col1=b.col1 and a.col2=b.col2
根本得不出需要的444 8。
只能用minus,个人意见!
---------- ----------
123 1
123 2
123 3
444 8
444 8
247 6
852 5已选择7行。SQL> select * from b;ID NAME
---------- ----------
123 1
123 3
444 8
852 5SQL> select * from a
2 minus
3 select * from b;ID NAME
---------- ----------
123 2
247 6SQL> select row_number() over(partition by a.id,a.name order by a.id,a.name) rn,a.id,a.name from a
2 minus
3 select row_number() over(partition by b.id,b.name order by b.id,b.name) rn,b.id,b.name from b; RN ID NAME
---------- ---------- ----------
1 123 2
1 247 6
2 444 8
这个上面的言论不负责任,没有测试。
现经过测试,发现minus确实会把444 8 也过滤掉,不好意思。
下面的那个sql是根据两条数据是一致的,那么就人为给他个
顺序号,这样就变成不一样的记录了,自然可以分辨出来。
----------------------------- ----------
1 a
2 ba
3 ca
4 daSQL> select * from b; B1 B2
----------------------------- ----------
1 a
2 bba
3 ca
5 dda
6 Eda
1 a6 rows selectedSQL>
SQL> select * from (
2 select b1,b2,row_number() over (partition by b1 order by b1) from b
3 minus
4 select a1,a2,row_number() over (partition by a1 order by a1) from a); B1 B2 ROW_NUMBER()OVER(PARTITIONBYB1
---------- ---------- ------------------------------
1 a 2
2 bba 1
5 dda 1
6 Eda 1SQL> 这个倒是可以实现,不过效率可能不是很好