【问题标题】:SQL select of records from table A but not in table BSQL从表A中选择记录但不在表B中
【发布时间】:2022-01-09 18:28:21
【问题描述】:

拜托,谁能帮我找到那个 SQL 语句 选择 TABLE_A 中的所有记录,除了 FIELD_1、FIELD_2 的组合不在 TABLE_B 中的记录。

(在 DB2 中)

【问题讨论】:

  • 表格有,而不是记录或字段。

标签: sql select db2


【解决方案1】:

你可以简单地使用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)

【讨论】:

    【解决方案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)
    

    【讨论】:

      【解决方案3】:

      您可以使用反连接。例如:

      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
      

      【讨论】:

      • @jarlh 感谢您指出这一点。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-31
      • 2017-08-08
      • 2013-05-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多