完全外部联接
这个问题并没有给我们太多的帮助,所以我几乎没有对数据做出任何假设。似乎没有“名称”表,这 3 个表中的任何一个似乎都不能代表完整的名称列表。所以,我要闭上眼睛,输入几个完整的外连接:
Select t1.name, t1.country, t2.food, t3.movie
from table1 t1
full outer join table2 t2 on t1.name = t2.name
full outer join table3 t3 on t3.name = t1.name
这将为您提供所有名称(不假设任何一个表都需要包含完整列表),每个表只列出一次(前提是它们还没有出现多次在任何一个表中)以及来自三个表中的任何一个的与该名称关联的任何数据。
更新
所以这里有一个更新(见下面的评论),它用“是”或“否”替换了国家、食物和电影的名称。
Select
t1.name,
case when t1.country is null then 'no' else 'yes' end as country,
case when t2.food is null then 'no' else 'yes' end as food,
case when t3.movie is null then 'no' else 'yes' end as movie
from table1 t1
full outer join table2 t2 on t1.name = t2.name
full outer join table3 t3 on t3.name = t1.name
...并用通用 Column1、Column2 重写,以防出现问题:
Select
t1.Column1 as name,
case when t1.Column2 is null then 'no' else 'yes' end as country,
case when t2.Column2 is null then 'no' else 'yes' end as food,
case when t3.Column2 is null then 'no' else 'yes' end as movie
from table1 t1
full outer join table2 t2 on t1.Column1 = t2.Column1
full outer join table3 t3 on t3.Column1 = t1.Column1
我希望这会有所帮助。