两个subselect into "IN" (oracle, sql)
Two subselect into "IN" (oracle, sql)
如何使用 into "IN" 两个 subselect?
我现在有:
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';
可以吗?或者也许是一些工会?
您可以使用 union
:
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 是一个值而不是一个列,因为您不能像您在问题中提到的那样使用列名来搜索值
I need too also that colC will be into second subselect:
如果你需要它在两个表中,那么你可以这样做:
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';
如何使用 into "IN" 两个 subselect?
我现在有:
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';
可以吗?或者也许是一些工会?
您可以使用 union
:
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 是一个值而不是一个列,因为您不能像您在问题中提到的那样使用列名来搜索值
I need too also that colC will be into second subselect:
如果你需要它在两个表中,那么你可以这样做:
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';