我正在尝试运行一个使用EXIST子句的查询:
select <...>
from A, B, C
where
A.FK_1 = B.PK and
A.FK_2 = C.PK and
exists (select A.ID from <subquery 1>) or
exists (select A.ID from <subquery 2>)
很遗憾,这似乎不被支持。我也尝试用IN
子句替换EXISTS
子句:
select <...>
from A, B, C
where
A.FK_1 = B.PK and
A.FK_2 = C.PK and
A.ID in (select ID from ...) or
A.ID in (select ID from ...)
不幸的是,IN
子句似乎也不受支持。
有什么想法可以编写SQL查询以实现所需的结果吗?原则上我可以将WHERE
子句建模为另一个JOIN
,并将第二个OR
子句建模为UNION
,但这似乎非常笨拙。
编辑:列出若干个可能的解决方案。
解决方案1
select <...>
from A, B, C
(select ID from ...) as exist_clause_1,
(select ID from ...) as exist_clause_2,
where
A.FK_1 = B.PK and
A.FK_2 = C.PK and
A.ID = exist_clause_1.ID or
A.ID = exist_clause_2.ID
解决方案 2
select <...>
from A, B, C
( (select ID from ...) UNION
(select ID from ...)
) as exist_clause,
where
A.FK_1 = B.PK and
A.FK_2 = C.PK and
A.ID = exist_clause.ID
exists (select E.ID from <subquery> E where E.ID = A.ID))
的 EXISTS 调用吗? - philipxy