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