两个subselect进入“IN”(oracle,sql)

时间:2018-03-23 13:18:17

标签: sql oracle12c sql-in

如何使用“IN”两个子选项?

我现在有:

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 个答案:

答案 0 :(得分:3)

您可以使用union

where colC in
(
    select T1 from tableT1 where colx = 'Y'
    union
    select T2 from tableT2 where colx = 'Y'
)
and ColD = 'Y'

答案 1 :(得分:1)

  

我也需要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';

答案 2 :(得分:0)

尝试此查询

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是一个值而不是列,因为你不能在你的问题中提到的值搜索值的地方使用列名