我試圖運行使用EXIST子句的查詢:星火替代EXISTS和IN
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
您的第一個查詢模板不應該是EXIST調用形式存在(選擇E.ID從 E其中E.ID = A.ID))? –
philipxy