三张表的联合和左连接
Union and left join of three tables
我有三个tableA、B、AB。
TableA有60个手机号码,每个手机号码得分A。
Table B有40个手机号码,每个手机号码一个分数B。
Table AB 有 40 个 A 的手机号码和 10 个 B 的手机号码以及一个 scoreAB。
如果手机只存在于TableA中,那么它的得分只有A。
如果一个手机只存在于Table B中,那么它的得分只有B。
如果一个手机同时存在于A和B中,那么它就有scoreA,scoreB,scoreAB
我想使用以下架构创建一个新的 table:
mobile scoreA scoreB scoreAB
其中,从TableA的60个手机号中,TableAB中不存在的20个手机号scoreB,scoreAB应该为空值。 Table AB 中存在的 剩余 40 个应该具有所有三个分数。
在 Table B 的 40 个手机号码中,Table AB 中不存在的 30 个手机号码的 scoreA、scoreAB 应该为空值。 Table AB 中剩余的 10 个应该具有所有三个分数。
截至目前,我做了以下事情:
spark.sql(""" SELECT A.mobile as mobile_A
FROM A
UNION
SELECT B.mobile as mobile_B
FROM B
UNION
SELECT AB.mobile as mobile_AB
FROM AB
""").createOrReplaceTempView('union_table')
然后,
spark.sql(""" SELECT u.mobile_A,
A.score as scoreA, B.score as scoreB, AB.score as scoreAB
FROM union_table as u
LEFT JOIN A
ON A.mobile = u.mobile_n
LEFT JOIN B
ON B.mobile = u.mobile_n
LEFT JOIN C
ON C.mobile = u.mobile_n
""").createOrReplaceTempView('scores_combined_table')
我没有得到正确的结果。上面的查询有什么问题?
一种方法使用 union all
和聚合:
select mobile,
max(scorea) as scorea,
max(scoreb) as scoreb,
max(scoreab) as scoreab
from ((select mobile, scorea, null as scoreb, null as scoreab
from a
) union all
(select mobile, null as scorea, scoreb, null as scoreab
from b
) union all
(select mobile, null as scorea, null as scoreb, scoreab
from ab
)
) ab
group by mobile;
这是实施 full join
的一个版本 -- MySQL 不支持。另一种方法使用 left join
:
select *
from (select mobile from a
union -- on purpose to remove duplicates
select mobile from b
union
select mobile from ab -- just in case there are other mobiles here
) m left join
a
using (mobile) left join
b
using (mobile)
left join
c
using (mobile);
我有三个tableA、B、AB。
TableA有60个手机号码,每个手机号码得分A。 Table B有40个手机号码,每个手机号码一个分数B。 Table AB 有 40 个 A 的手机号码和 10 个 B 的手机号码以及一个 scoreAB。
如果手机只存在于TableA中,那么它的得分只有A。
如果一个手机只存在于Table B中,那么它的得分只有B。
如果一个手机同时存在于A和B中,那么它就有scoreA,scoreB,scoreAB
我想使用以下架构创建一个新的 table:
mobile scoreA scoreB scoreAB
其中,从TableA的60个手机号中,TableAB中不存在的20个手机号scoreB,scoreAB应该为空值。 Table AB 中存在的 剩余 40 个应该具有所有三个分数。
在 Table B 的 40 个手机号码中,Table AB 中不存在的 30 个手机号码的 scoreA、scoreAB 应该为空值。 Table AB 中剩余的 10 个应该具有所有三个分数。
截至目前,我做了以下事情:
spark.sql(""" SELECT A.mobile as mobile_A
FROM A
UNION
SELECT B.mobile as mobile_B
FROM B
UNION
SELECT AB.mobile as mobile_AB
FROM AB
""").createOrReplaceTempView('union_table')
然后,
spark.sql(""" SELECT u.mobile_A,
A.score as scoreA, B.score as scoreB, AB.score as scoreAB
FROM union_table as u
LEFT JOIN A
ON A.mobile = u.mobile_n
LEFT JOIN B
ON B.mobile = u.mobile_n
LEFT JOIN C
ON C.mobile = u.mobile_n
""").createOrReplaceTempView('scores_combined_table')
我没有得到正确的结果。上面的查询有什么问题?
一种方法使用 union all
和聚合:
select mobile,
max(scorea) as scorea,
max(scoreb) as scoreb,
max(scoreab) as scoreab
from ((select mobile, scorea, null as scoreb, null as scoreab
from a
) union all
(select mobile, null as scorea, scoreb, null as scoreab
from b
) union all
(select mobile, null as scorea, null as scoreb, scoreab
from ab
)
) ab
group by mobile;
这是实施 full join
的一个版本 -- MySQL 不支持。另一种方法使用 left join
:
select *
from (select mobile from a
union -- on purpose to remove duplicates
select mobile from b
union
select mobile from ab -- just in case there are other mobiles here
) m left join
a
using (mobile) left join
b
using (mobile)
left join
c
using (mobile);