Union和Union All到底有什么区别
来源:互联网 发布:nosql数据库的优点 编辑:程序博客网 时间:2024/05/01 14:46
以前一直不知道Union和Union All到底有什么区别,今天来好好的研究一下,网上查到的结果是下面这个样子,可是还是不是很理解,下面将自己亲自验证:
Union:对两个结果集进行并集操作,不包括重复行,同时进行默认规则的排序;
Union All:对两个结果集进行并集操作,包括重复行,不进行排序;
下面进行简单的测试(因为是测试,所以sql代码写的很简单,没有什么很严格的规范,只是为了理解这两者之间的区别)
严格的标准写法应该先判断数据库是否存在,表是否存在等等约束
第一步,建库:
- Create database Test
- go
- use Test
- go
第二步,建表:
- Create table Table1
- (
- id int not null,
- name varchar(20) not null
- )
- Create table Table2
- (
- id int not null,
- name varchar(20) not null
- )
第三步,插入测试数据:
- Insert into Table1 values (1,'姚羽')
- Insert into Table1 values (2,'边兵兵')
- Insert into Table1 values (3,'袁磊')
- Insert into Table2 values (1,'姚羽')
- Insert into Table2 values (2,'柳春平')
- Insert into Table2 values (3,'张永超')
- Insert into Table2 values (4,'刘华健')
第四步,测试开始:
- select * from Table1
- select * from Table2
执行两个表的查询结果如下
可以很容易的看到,上面插入的测试数据当中,有一条是重复的
那么我们 先看执行union 看看
- select * from Table1
- union
- select * from Table2
再执行union all 看看
- select * from Table1
- union all
- select * from Table2
相信到此时,应该明白了union 和 union all 的区别了, 我以前也一直没搞清楚,这次看视频,就终于搞清楚了
0 0
- Union和Union All到底有什么区别
- Union和Union All到底有什么区别
- Union和Union All到底有什么区别
- Union和Union All到底有什么区别
- union和union all有什么不同
- union和union all有什么不同?
- union和union all有什么不同?
- union 和union all 区别
- Union和Union all区别
- union 和 union all 区别
- union和union all区别
- UNION和UNION ALL区别
- union和union all区别
- Union 和Union All区别
- Union和Union All区别
- union all和union区别
- union和union all区别
- union , union all 区别
- java反射总结
- Fast rcnn and Faster rcnn 算法原理
- 【行人检测】之Joint Deep Learning联合深度学习
- 如何用几何画板动态演示电饭锅工作原理
- HDU5961(bitset,暴力)
- Union和Union All到底有什么区别
- 05 Java循环
- 在Swift中如何使用C中的Struct
- iOS 获取系统通讯录的两种方法
- UE4 VR 手柄代替鼠标滚轮(HTC VIVE)
- dwr的介绍和实例
- 一天一条Linux指令-killall
- 详解.htm.html.shtm.shtml的区别与联系
- 对系统圆形进度条的改进,使首尾圆滑接触