日期:2014-05-16  浏览次数:20432 次

oracle的left join,right join和full join的一点介绍

    以下是摘自oracle ocp9i文档:
      outer join syntax:
      1)you use an outer join to also see rows that do not meet the join condition
      2)the outer join operator is the plus sign(+)
     outer join restrictions:
     1)the outer join operator can appear on only one side of the expression:the side that has information missing.it returns those rows from one table that have no direct match in the other table.
     2)a condition involving an outer join cannot use the IN operator or be linked to another condition by the OR operator.

    配置实验环境:

hr@ORCL> drop table a;
hr@ORCL> drop table b;
hr@ORCL> create table a(id number,name varchar2(10));
hr@ORCL> create table b(id number,name varchar2(10));
hr@ORCL> insert into a values(1,'a');
hr@ORCL> insert into a values(2,'b');
hr@ORCL> insert into a values(3,'c');
hr@ORCL> insert into b values(1,'a');
hr@ORCL> insert into b values(2,'b');
hr@ORCL> insert into b values(4,'d');
hr@ORCL> select * from a;

        ID NAME
---------- ----------
         1 a
         2 b
         3 c

hr@ORCL> select * from b;

        ID NAME
---------- ----------
         1 a
         2 b
         4 d

hr@ORCL> commit;


    --全外连接的结果是:
      1)选出所有满足条件的结果
      2)以左表为准,将左表不满足条件的结果接在左边
      3)以右表为准,把右表不满足条件的结果接在右边
      4)将以上结果全部合起来

hr@ORCL> select a.id,b.id from a full join b on a.id=b.id;

        ID         ID
---------- ----------
         1          1
         2          2
         3
                    4


    --left out join和oracle的加号在右结果是相同。同理,right out join和加号在左是一样的。(sql99的语法和oracle私有语法的比较)

hr@ORCL> select a.id,b.id from a,b where a.id=b.id(+);

        ID         ID
---------- ----------
         1          1
         2          2
         3

hr@ORCL> select a.id,b.id from a left outer join b on a.id=b.id;

        ID         ID
---------- ----------
         1          1
         2          2
         3


    --结果类似于from a left join b on a.col=b.col and a.coln=....。单个列选择条件的列是基表(加号在谁身上谁是从表,没有加号的一方是基表)的用decode和+改写,不能用一般的改写,若不是基表的可以简单用+改写。比如:

SQL> SELECT a.ID,b.ID
  2  FROM a FULL JOIN b
  3  ON a.ID=b.ID AND a.NAME='a';

        ID         ID
---------- ----------
         1          1
         2 
         3 
         1          1
                    4
                    2

改写之:

SQL> SELECT a.ID,b.ID
  2  FROM a,b WHERE a.id=decode(a.NAME,'a',b.ID(+))
  3  UNION ALL
  4  SELECT a.ID,b.ID
  5  FROM a,b WHERE a.ID(+)=b.ID AND a.NAME(+)='a' AND a.ID IS NULL;

        ID         ID
---------- ----------
         1          1
         2 
         3 
         1          1
                    2
                    4


    用union来实现上面例子的full join结果,需要考虑表的关系。

    1)如果是两表1:1
         加号在左,以右为准(相当于sql99的right join)
         加号在右,以左为准(相当于sql99的left join)

hr@ORCL> select a.id,b.id from a,b where a.id=b.id(+)
  2  union
  3      select a.id,b.id from a,b where a.id(+)=b.id;

        ID         ID
---------- ----------
         1          1
         2          2
         3
                    4


    2)如果是两表1:n,用union剔重就不正确。

hr@ORCL> insert into a values(1,'a');
hr@ORCL> commit;
hr@ORCL> select * from a;

        ID NAME
---------- ----