【问题标题】:Full join is not giving me the desired result完全加入没有给我想要的结果
【发布时间】:2021-04-18 16:18:48
【问题描述】:

我正在尝试加入下面的 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代码吗?

【问题讨论】:

  • 向我们展示未给您预期结果的查询。

标签: sql join outer-join


【解决方案1】:

你似乎知道答案:full join:

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

这是标准 SQL 功能。

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

    • 哪个 dbms 有 OUTER 作为强制关键字?
    猜你喜欢
    • 1970-01-01
    • 2020-08-06
    • 2021-08-29
    • 1970-01-01
    • 2015-06-11
    • 1970-01-01
    • 2013-10-26
    • 2021-03-02
    • 1970-01-01
    相关资源
    最近更新 更多