【发布时间】:2022-01-09 18:28:21
【问题描述】:
拜托,谁能帮我找到那个 SQL 语句 选择 TABLE_A 中的所有记录,除了 FIELD_1、FIELD_2 的组合不在 TABLE_B 中的记录。
(在 DB2 中)
【问题讨论】:
-
表格有行和列,而不是记录或字段。
拜托,谁能帮我找到那个 SQL 语句 选择 TABLE_A 中的所有记录,除了 FIELD_1、FIELD_2 的组合不在 TABLE_B 中的记录。
(在 DB2 中)
【问题讨论】:
你可以简单地使用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)
【讨论】:
使用不存在:
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)
【讨论】:
您可以使用反连接。例如:
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
【讨论】: