【问题标题】:How to perform FULL OUTER JOIN in ORACLE using '+' operator?如何使用“+”运算符在 ORACLE 中执行 FULL OUTER JOIN?
【发布时间】:2012-05-08 13:54:09
【问题描述】:

除了使用 FULL OUTER JOIN 或 FULL JOIN 之类的关键字,如何在 '+' 运算符的帮助下使用 'where' 子句执行完全外部连接?!

【问题讨论】:

  • 您必须将 2 个外部连接查询的结果合并在一起(一个用于 A -> B,一个用于 B -> A)
  • 您为什么要这样做?使用显式连接,这是首选方法。隐式连接区域 SQL 反模式。
  • 您为什么要这样做?只需使用 FULL OUTER JOIN 语法(强烈建议使用显式 ANSI 样式连接而不是隐式连接)

标签: sql oracle


【解决方案1】:

你不能(至少直接)。 Oracle 仅支持使用 SQL:1999 语法的完全外连接。

你可以通过联合两个外连接来伪造它:

select a.field1, b.field2
from table_a a, table_b b
where a.id = b.id(+)
union all 
select a.field1, b.field2
from table_a a, table b b
where a.id(+) = b.id
      and a.id is null

使用 SQL:1999 语法更具可读性:

select a.field1, b.field2
from table_a a full outer join table_b b
on a.id = b.id

【讨论】:

    【解决方案2】:

    这是一个您也可以在 oracle 中运行以查看结果的示例。

    with 
    a as 
       (select 'A' tbl, level id from dual connect by level < 1000),
    b as 
       (select 'B' tbl, level + 500 id from dual connect by level < 1000)
    select a.tbl, a.id, b.tbl, b.id from a, b where a.id = b.id(+)
    union all
    select a.tbl, a.id, b.tbl, b.id from a, b where a.id(+) = b.id and a.id is null
    

    等同于:

    with 
    a as 
       (select 'A' tbl, level id from dual connect by level < 1000),
    b as 
       (select 'B' tbl, level + 500 id from dual connect by level < 1000)
    select a.tbl, a.id, b.tbl, b.id from a full outer join b on a.id = b.id
    

    【讨论】:

      猜你喜欢
      • 2022-08-30
      • 1970-01-01
      • 2020-03-03
      • 2021-09-06
      • 1970-01-01
      相关资源
      最近更新 更多