我在做两张表连接时, 举个例子:A 表有个字段是A_ID,B表有个字读也是A_ID,我现在想把A表中不在B表中的记录挑出来,也就是NOT IN 操作,因为NOT IN 效率很低,我用了DB2的 LEFT JOIN ON A.A_ID<>;B.A_ID,但结果不对,以前在用ORACLE时候,用a.a_id=b.a_id(+) and b.a_id is null 就可以了,不知道在DB2中该如何做才能达到目的。 很急,请大家帮个忙,怎么做,谢谢!
Law 回复于:2002-12-08 11:56:30
db2里面也有相应比较快的方法,就是使用except(EXCEPT 运算符通过包括所有在 TABLE1 中但不在 TABLE2 中的行并消除所有重复行而派生出一个结果表。当 ALL 随 EXCEPT 一起使用时 (EXCEPT ALL),不消除重复行。 )。命令如下:
select A_ID from B except all select A_ID from A
yuly 回复于:2002-12-08 13:30:55
谢谢law解决了我的燃眉之急,谢谢!
yuly 回复于:2002-12-09 13:01:59
那么如何把A_ID对应的其它字段选出来呢?当然可以用IN谓词,但有其他办法没有? select * from A where A_ID in (select A_ID from A except all select A_ID from B)
vlife 回复于:2002-12-09 17:57:47
select * from A where A_ID not in(select * from A_ID from b)
dennisfox 回复于:2002-12-11 19:05:48
select * from (select A.*,B.A_ID B_AID from A left join B on A.A_ID=B.A_ID) as temp where temp.B_AID is not null
dennisfox 回复于:2002-12-11 19:07:12
select * from (select A.*,B.A_ID B_AID from A left join B on A.A_ID=B.A_ID) as temp where temp.B_AID is null ------------------------------Mod--------------
Y2015 回复于:2003-03-19 19:02:00
select * from a where not exists (select id from b)
|