完全加入没有给我想要的结果

问题描述

我正在尝试加入下面的 2 个表

表 1:

| Name | city   | Country | Enabled |
|------|--------|---------|---------|
| Alex | London | UK      | 1       |
| Burt | Berlin | DE      | 0       |
| Carl | Moscow | RU      | 1       |

表 2:

| Name  | post code | street |
|-------|-----------|--------|
| Burt  | 1234      | Road1  |
| Darek | 5678      | Road2  |

想要的结果

| Name  | city_t1 | country_t1 | enabled_t1 | post_code | Street |
|-------|---------|------------|------------|-----------|--------|
| Alex  | London  | UK         | 1          | NULL      | NULL   |
| Burt  | Berlin  | DE         | 0          | 1234      | Road1  |
| Carl  | Moscow  | RU         | 1          | NULL      | NULL   |
| Darek | NULL    | NULL       | NULL       | 5678      | Road2  |

有人可以帮我提供 sql 代码吗?

解决方法

您似乎知道答案:full join

select *
from table1 t1 full join
     table2 t2
     using (name);

这是标准的 SQL 功能。

,

请发布您要执行的 SQL。 正如我这里的朋友所建议的那样,它应该可以工作。 在某些环境中,您应该使用“完全外连接”语法。

select *
from table1 t1 full outer join
     table2 t2
     using (name);