db2 从表A中但不在表B中选择记录的SQL

8i9zcol2  于 2022-11-07  发布在  DB2
关注(0)|答案(3)|浏览(187)

请有人帮我找到选择TABLE_A中所有记录的SQL语句,除了那些FIELD_1、FIELD_2的组合不在TABLE_B中的记录。
(In DB2)的数据库

eeq64g8w

eeq64g8w1#

您可以简单地使用NOT EXISTS

select * from table_a ta
where not exists (select * from table_b tb
                  where tb.field_1 = ta.field_1
                    and tb.field_2 = ta.field_2)
c7rzv4ha

c7rzv4ha2#

使用不存在:

select * from table_A where not exists (select * from table_B 
                  where table_B.field_1 = table_A.field_1
                    and  table_B.field_2 = ta.field_2)
68de4m5k

68de4m5k3#

可以使用反联接。例如:

select *
from table_a a
left join table_b b on b.field_1 = a.field_1 and b.field_2 = a.field_2
where b.field_1 is null

相关问题