理解full outer jion,union,union all

准备两张测试表
09:41:52 kiwi@storedb1> select * from t1;

          ID1 I
------------- -
            1 A
            2 B
            3 C

Elapsed: 00:00:00.00
09:41:56 kiwi@storedb1> select * from t2;

I ID
- --
A A2
B B2
D D2

我们先来看看full outer join
09:42:14 kiwi@storedb1> select t1.id1,t1.id2,t2.id3 from t1 full outer join t2 on(t1.id2=t2.id2);

          ID1 I ID
------------- - --
            1 A A2
            2 B B2
                D2
            3 C
            
再来看看union
09:44:54 kiwi@storedb1>  select t1.id1,t1.id2,t2.id3 from t1 left outer join  t2 on(t1.id2=t2.id2)
09:45:09   2  union
09:45:18   3   select t1.id1,t1.id2,t2.id3 from t1 right outer join  t2 on(t1.id2=t2.id2);

          ID1 I ID
------------- - --
            1 A A2
            2 B B2
            3 C
                D2

看看 union all
  1   select t1.id1,t1.id2,t2.id3 from t1 left outer join  t2 on(t1.id2=t2.id2)
  2  union all
  3*  select t1.id1,t1.id2,t2.id3 from t1 right outer join  t2 on(t1.id2=t2.id2)
09:46:20 kiwi@storedb1> /

          ID1 I ID
------------- - --
            1 A A2
            2 B B2
            3 C
            1 A A2
            2 B B2
                D2

从中我们就可以看出区别full outer join 相当于分别做left outer join,right outer join 然后做一个union
union 是两个的交集不包括重复的行,默认进行排序
而union all的效果是left outer join和right outer join的交集,包括了重复的集合,不进行排序

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值