【FULL OUTER JOIN】全外连接的union all改写方法

本文通过三种不同的方式展示了在Oracle数据库中如何实现全外连接,并对比了它们的语法特点。

对于SQL中的连接操作在实现业务需求的时候比较方便和高效,这里针对“全外连接”展示一下在Oracle中的几种写法。
每种写法因人而异,以满足需求为目的。

有关内连接,左连接和右连接的简单演示请参考:《【实验】内连接,左连接,右连接,全外连接》http://space.itpub.net/519536/viewspace-563019

1.创建实验表并初始化实验数据
SQL> create table a (a number(1),b number(1),c number(1));
SQL> create table b (a number(1),d number(1),e number(1));
SQL> insert into a values(1,1,1);
SQL> insert into a values(2,2,2);
SQL> insert into a values(3,3,3);
SQL> insert into b values(1,4,4);
SQL> insert into b values(2,5,5);
SQL> insert into b values(4,6,6);
SQL> commit;

2.查看一下初始化数据内容
sec@ora10g> select * from a;

         A          B          C
---------- ---------- ----------
         1          1          1
         2          2          2
         3          3          3

sec@ora10g> select * from b;

         A          D          E
---------- ---------- ----------
         1          4          4
         2          5          5
         4          6          6

3.第一种写法,这也是标准SQL的写法。功能明确,不过理解有点小障碍。
sec@ora10g> select * from a full outer join b on a.a = b.a;

         A          B          C          A          D          E
---------- ---------- ---------- ---------- ---------- ----------
         1          1          1          1          4          4
         2          2          2          2          5          5
         3          3          3
                                          4          6          6

4.第二种写法
思路:“a到b的全外连接”=“a到b的内连接” + “b到a的反连接” + “a到b的反连接”。

sec@ora10g> select *
  2    from a, b
  3   where a.a = b.a
  4  union all
  5  select *
  6    from a, b
  7   where a.a(+) = b.a
  8     and a.a is null
  9  union all
 10  select *
 11    from a, b
 12  where a.a = b.a(+)
 13     and b.a is null
 14  /

         A          B          C          A          D          E
---------- ---------- ---------- ---------- ---------- ----------
         1          1          1          1          4          4
         2          2          2          2          5          5
                                          4          6          6
         3          3          3

5.第三种写法
思路:“a到b的全外连接”=“b到a的外部连接” + “a到b的反连接”。
sec@ora10g> select *
  2    from a, b
  3   where a.a(+) = b.a
  4  union all
  5  select *
  6    from a, b
  7   where a.a = b.a(+)
  8     and b.a is null
  9  /

         A          B          C          A          D          E
---------- ---------- ---------- ---------- ---------- ----------
         1          1          1          1          4          4
         2          2          2          2          5          5
                                          4          6          6
         3          3          3


6.小结
注意,改写方法中的“union all”也可以修改为“union”,之所以上面改写方法中我们使用union all,原因是出于性能方便的考虑,union all的执行效率远大于union操作

这里展示的是一种转换思路,
可一看了之。

-- The End --

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值