代码之家  ›  专栏  ›  技术社区  ›  Martin Čuka

选择表A减去表B,其中条件由两列组成

  •  1
  • Martin Čuka  · 技术社区  · 6 年前

    我有两张桌子。表A和表B。

    我想从表A中选择不在表B中的所有内容。 听起来很简单,关键是我需要根据两个值(两列)来选择它

    select a.revision, a.casetype from A a
    minus
    select b.revision, b.casetype from B b;
    

    问题是我无法从表A中获取ID。

    我也试着写一些像下面的查询,但我想我不能这样做,因为我需要检查修订和案件类型一起

    select * from A a where a.casetype IN (select...) and a.revision IN (select...)
    

    你知道怎么解决吗?谢谢

    3 回复  |  直到 6 年前
        1
  •  4
  •   John Humphreys    6 年前

    当然,我相信一个基本的不存在检查应该工作。

    select a.id, a.revision, a.casetype
    from A a
    where not exists (
        select 1
        from B
        where revision = a.revision and casetype = a.casetype
    );
    
        2
  •  3
  •   Gordon Linoff    6 年前

    Oracle支持元组,因此如果需要,可以执行以下操作:

    select a.*
    from a
    where (a.revision, a.casetype) in (select a.revision, a.casetype from A a
                                       minus
                                       select b.revision, b.casetype from B b
                                      );
    

    not exists ,但这是建立在您已经完成的基础上的解决方案。

        3
  •  0
  •   Ramkumar Sambandam    6 年前

    除了应该有用

    除了 从b中选择b.revision、b.casetype;