如何使用'+'运算符在ORACLE中执行FULL OUTER JOIN?

时间:2012-05-08 13:54:09

标签: sql oracle

我没有使用FULL OUTER JOIN或FULL JOIN等关键字,而是如何在'+'运算符的帮助下使用'where'子句执行完全外连接?!

2 个答案:

答案 0 :(得分:21)

你不能(至少直接)。 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

答案 1 :(得分:3)

以下是您可以在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
相关问题