首页 > 解决方案 > 如何将 OR 约束与多个半连接一起使用?

问题描述

如何在 SparkSQL 或 Dataframe API 中使用 semijoin 实现以下 SQL?

select * 
from foo
left join a on foo.id = a.id
left join b on foo.id = b.id
where exists (select 1 from a x where foo.id = x.id)
or exists (select 1 from b x where foo.id = x.id)
;

我已经尝试过了,但它的功能是 AND

.join(loincDF, foo("id") <=> a("id"), "leftsemi")
.join(loincDF, foo("id") <=> b("id"), "leftsemi")

标签: apache-sparkapache-spark-sql

解决方案


你可以尝试这样的事情:

foo.join(a, Seq("id"), "leftsemi")
   .union( foo.join(b, Seq("id"), "leftsemi") )
   .distinct

推荐阅读