全外连接的union all改写方法是什么样的

发布时间:2021-11-04 17:38:20 作者:柒染
来源:亿速云 阅读:232

这篇文章给大家介绍全外连接的union all改写方法是什么样的,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。

对于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操作

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

关于全外连接的union all改写方法是什么样的就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

推荐阅读:
  1. 什么是mysql全外连接
  2. union 与 union all的区别 ,union排除

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

union all

上一篇:分析iOS中的多继承与多重代理

下一篇:分析iOS自适应cell行高

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》