I am trying to run a query that uses the EXIST clause:
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>)
Unfortunately, this does not seem to be supported. I have also tried replacing the EXISTS
clause with an IN
clause:
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 ...)
Unfortunately, also the IN
clause seems to be unsupported.
Any ideas of how I can write a SQL query that achieves the desired result? I could model in principle the WHERE
clause as another JOIN
and the second OR
clause as an UNION
but it seems super clumsy..
EDIT: Listing a number of possible solutions.
Solution 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
Solution 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
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…