Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
两个子项选入“中”;在;(oracle,sql)_Sql_Oracle12c_Sql In - Fatal编程技术网

两个子项选入“中”;在;(oracle,sql)

两个子项选入“中”;在;(oracle,sql),sql,oracle12c,sql-in,Sql,Oracle12c,Sql In,如何在两个子选择中使用into 我现在: 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 f

如何在两个子选择中使用into

我现在:

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';

有可能吗?或者某个联合体?

您可以使用
联合体:

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

您可以使用
联合体

where colC in
(
    select T1 from tableT1 where colx = 'Y'
    union
    select T2 from tableT2 where colx = 'Y'
)
and ColD = 'Y'
请尝试此查询

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 case中使用列名来搜索您在问题中提到的值

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中使用列名来搜索您在问题中提到的值

我还需要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';
如果需要在两个表中都显示,则可以执行以下操作:

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';
如果需要将其放在一个或另一个(或两个)表中,则可以执行以下操作:

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';
我还需要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';
如果需要在两个表中都显示,则可以执行以下操作:

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';
如果需要将其放在一个或另一个(或两个)表中,则可以执行以下操作:

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';

colC-in或colC-in
请重新表述您的问题,提供更多的上下文,并更好地解释您试图完成的任务。
colC-in或colC-in
请重新表述您的问题,提供更多的上下文,以及更好地解释您试图实现的目标。使用
UNION ALL
是否更有效?OP询问“我也需要colC将进入第二个子选择”-OP的语言并不完全清楚,但这将给出“colC IN(第一个子选择)或colC IN(第二个子选择)”而我认为OP的问题需要“colC IN(第一次选择)和colC IN(第二次选择)”。@MT0:如果必须在两个子集中,可以使用
intersect
而不是
union
。同意
union all
会更有效。谢谢@andomarw使用
union all
会更有效吗?OP问“我也需要colC将进入第二个子选择”-OP的语言不完全清楚,但会给出“colC IN(第一个子选择)或colC IN(第二个子选择)”而我认为OP的问题需要“colC IN(第一次选择)和colC IN(第二次选择)”。@MT0:如果必须在两个子集中,可以使用
intersect
而不是
union
。同意“联合所有人”
会更有效率。谢谢@Andomar