简体   繁体   English

如何使用ORACLE JOIN(+)查找LEFT OUTER JOIN或RIGHT OUTER JOIN

[英]How to find LEFT OUTER JOIN or RIGHT OUTER JOIN with ORACLE JOIN (+)

I am confused with finding left outer join and right outer join properly with Oracle join (+) sign. 我对用Oracle联接(+)符号正确找到左外部联接和右外部联接感到困惑。 Check this vs this . 检查这个这个 I feel both contradict. 我觉得两者矛盾。 What I understand is, First link says if the (+) sign is in right hand side, it will be Right Outer Join. 我的理解是,第一个链接说,如果(+)号在右侧,它将是“ Right Outer Join”。

Whereas with second link, my understanding is totally wrong. 而第二个链接,我的理解是完全错误的。

Please clarify how to find the right and left outer join properly with an example? 请举例说明如何正确找到左右外部连接?

Left outer join just means that you want to return all of the table on the left hand side of the join, and any matching rows from the table on the right. 左外部联接只是意味着您要返回联接左侧的所有表,以及右侧表中的所有匹配行。

In old-style Oracle syntax, that would be: where t1.col1 = t2.col1 (+) In ANSI syntax, that would be: from t1 left outer join t2 on (t1.col1 = t2.col1) 在旧式Oracle语法中,这将是: where t1.col1 = t2.col1 (+)在ANSI语法中将是: from t1 left outer join t2 on (t1.col1 = t2.col1)

Right outer join means that you want to return all of the table on the right hand side of the join, and any matching rows from the table on the left. 右外部联接意味着您想返回联接右侧的所有表,以及左侧表中的所有匹配行。

In old-style Oracle syntax, that would be: where t2.col1 (+) = t1.col1 In ANSI syntax, that would be: from t2 right outer join t1 on (t2.col1 = t1.col1) 在旧式Oracle语法中,这将是: where t2.col1 (+) = t1.col1在ANSI语法中,这将是: from t2 right outer join t1 on (t2.col1 = t1.col1)

You will, of course, have spotted that you can turn a right outer join into a left outer join simply by reversing the order of the tables. 您当然会发现,只需反转表的顺序即可将右外部联接转换为左外部联接。 Most outer joins are left ones, probably because it's easier to think of "I want all of this first table, and any matching rows from this other table" rather than the other way round. 大多数外部联接都是左联接,这可能是因为更容易想到“我想要所有第一个表以及该另一个表中的所有匹配行”,而不是相反。 YMMV, of course! YMMV,当然!


ETA the following examples: ETA以下示例:

Left Outer Join: 左外连接:

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   

Right Outer Join: 右外连接:

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   

Please clarify how to find the right and left outer join properly with an example 请举例说明如何正确找到左右外部连接

I will give a try to show the difference between Oracle outer join syntax and the ANSI/ISO Syntax . 我将尝试展示Oracle外部联接语法ANSI / ISO语法之间的区别。

LEFT OUTER JOIN - 左外连接-

SELECT e.last_name,
  d.department_name
FROM employees e,
  departments d
WHERE e.department_id = d.department_id(+);

SELECT e.last_name,
  d.department_name
FROM employees e
LEFT OUTER JOIN departments d
ON (e.department_id = d.department_id);

RIGHT OUTER JOIN - 右外连接-

SELECT e.last_name,
  d.department_name
FROM employees e,
  departments d
WHERE e.department_id(+) = d.department_id;

SELECT e.last_name,
  d.department_name
FROM employees e
RIGHT OUTER JOIN departments d
ON (e.department_id = d.department_id);

FULL OUTER JOIN - 外部连接-

Before the native support of hash full outerjoin in 11gR1, Oracle would internally convert the FULL OUTER JOIN the following way - 在11gR1中对散列完全外部联接的本地支持之前,Oracle将以以下方式在内部转换FULL OUTER JOIN-

SELECT e.last_name,
  d.department_name
FROM employees e,
  departments d
WHERE e.department_id = d.department_id(+)
UNION ALL
SELECT NULL,
  d.department_name
FROM departments d
WHERE NOT EXISTS
  (SELECT 1 FROM employees e WHERE e.department_id = d.department_id
  );

SELECT e.last_name,
  d.department_name
FROM employees e
FULL OUTER JOIN departments d
ON (e.department_id = d.department_id);

Have a look at this . 看看这个

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM