代码之家  ›  专栏  ›  技术社区  ›  ruipacheco

如何从表中选择第二个表中不存在的所有元素?

  •  0
  • ruipacheco  · 技术社区  · 7 年前

    我有两个表,A和B,其中B有A的外键。行被插入到A中,在A中的数据经过一些处理后,A行被插入到B中,外键是A。

    如何选择A中没有对应B行的所有行?

    2 回复  |  直到 7 年前
        1
  •  2
  •   Rams    7 年前

    您使用 not exists 关键字执行相同操作

    select key_column from A 
     where not exists (select 1 from B where b.foreign_key_column=a.key_column)
    
        2
  •  1
  •   Kacper    7 年前
    select key_column from A
    minus
    select foreign_key_column from B;
    

    这将为您提供a中存在但B中不存在的值的ID列表