【发布时间】:2016-01-18 18:23:22
【问题描述】:
我正在尝试运行使用 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 调用吗?
标签: sql apache-spark-sql