左外连接只是意味着您要返回连接左侧的所有表,以及右侧表中的所有匹配行。
在旧式 Oracle 语法中,这将是:where t1.col1 = t2.col1 (+)
在 ANSI 语法中,这将是:from t1 left outer join t2 on (t1.col1 = t2.col1)
右外连接意味着您要返回连接右侧的所有表,以及左侧表中的所有匹配行。
在旧式 Oracle 语法中,这将是:where t2.col1 (+) = t1.col1
在 ANSI 语法中,这将是:from t2 right outer join t1 on (t2.col1 = t1.col1)
您当然会发现,只需颠倒表的顺序,您就可以将右外连接变成左外连接。大多数外连接都是左连接,可能是因为更容易想到“我想要所有第一个表,以及来自另一个表的任何匹配行”,而不是相反。当然是 YMMV!
ETA 以下示例:
左外连接:
with t1 as (select 1 col1, 10 col2 from dual union all
select 2 col1, 20 col2 from dual union all
select 3 col1, 30 col2 from dual),
t2 as (select 1 col1, 100 col2 from dual)
select t1.*, t2.*
from t1, t2
where t1.col1 = t2.col1 (+)
order by t1.col1;
COL1 COL2 COL1_1 COL2_1
---------- ---------- ---------- ----------
1 10 1 100
2 20
3 30
with t1 as (select 1 col1, 10 col2 from dual union all
select 2 col1, 20 col2 from dual union all
select 3 col1, 30 col2 from dual),
t2 as (select 1 col1, 100 col2 from dual)
select t1.*, t2.*
from t1 left outer join t2 on (t1.col1 = t2.col1)
order by t1.col1;
COL1 COL2 COL1_1 COL2_1
---------- ---------- ---------- ----------
1 10 1 100
2 20
3 30
右外连接:
with t1 as (select 1 col1, 10 col2 from dual union all
select 2 col1, 20 col2 from dual union all
select 3 col1, 30 col2 from dual),
t2 as (select 1 col1, 100 col2 from dual)
select t1.*, t2.*
from t1, t2
where t2.col1 (+) = t1.col1
order by t1.col1;
COL1 COL2 COL1_1 COL2_1
---------- ---------- ---------- ----------
1 10 1 100
2 20
3 30
with t1 as (select 1 col1, 10 col2 from dual union all
select 2 col1, 20 col2 from dual union all
select 3 col1, 30 col2 from dual),
t2 as (select 1 col1, 100 col2 from dual)
select t1.*, t2.*
from t2 right outer join t1 on (t2.col1 = t1.col1)
order by t1.col1;
COL1 COL2 COL1_1 COL2_1
---------- ---------- ---------- ----------
1 10 1 100
2 20
3 30