代码之家  ›  专栏  ›  技术社区  ›  4est

将两个子项选择为“IN”(oracle、sql)

  •  1
  • 4est  · 技术社区  · 8 年前

    如何使用到 “在” 两个子选择?

    我现在有:

    select colA, colB from table1 where colC in
                     (select T1 from tableT1 where colx = 'Y')
                     and ColD = 'Y';
    

    我也需要 colC公司 将进入第二个子选择:

    select colA, colB from table1 where colC in
                     ((select T1 from tableT1 where colx = 'Y')
                      or
                     (select T2 from tableT2 where colx = 'Y'))
                     and ColD = 'Y';
    

    有可能吗?或者是某种联盟?

    3 回复  |  直到 8 年前
        1
  •  3
  •   Andomar    8 年前

    你可以使用 union :

    where colC in
    (
        select T1 from tableT1 where colx = 'Y'
        union
        select T2 from tableT2 where colx = 'Y'
    )
    and ColD = 'Y'
    
        2
  •  1
  •   MT0    8 年前

    我还需要colC进入第二个子选择:

    如果需要在两个表中都显示,则可以执行以下操作:

    select colA, colB
    from   table1
    where  colC in ( select T1 from tableT1 where colx = 'Y' )
    AND    colC in ( select T2 from tableT2 where colx = 'Y' )
    AND    ColD = 'Y';
    

    如果需要将其放在一个或另一个(或两个)表中,则可以执行以下操作:

    select colA, colB
    from   table1
    where  (  colC in ( select T1 from tableT1 where colx = 'Y' )
           OR colC in ( select T2 from tableT2 where colx = 'Y' )
           )
    AND    ColD = 'Y';
    
        3
  •  0
  •   MT0    8 年前

    尝试此查询

    select colA, colB from 
                     ((select T1 as T, ColA,colB from tableT1 where colx = 'Y')
                      Union
                     (select  T2 as T, ColA,colB from tableT2 where colx = 'Y'))  as table1 where table1.T = ColC
                     and ColD = 'Y';
    

    我假设Colc是一个值,而不是一列,因为您不能在where cause中使用列名来搜索您在问题中提到的值