sql中union和union all的区别
union 连接两个表查询的结果
假设我们有一个表Student,包括以下字段与数据:
- drop table student;
- create table student
- (
- id int primary key,
- name nvarchar2(50) not null,
- score number not null
- );
- insert into student values(1,'Aaron',78);
- insert into student values(2,'Bill',76);
- insert into student values(3,'Cindy',89);
- insert into student values(4,'Damon',90);
- insert into student values(5,'Ella',73);
- insert into student values(6,'Frado',61);
- insert into student values(7,'Gill',99);
- insert into student values(8,'Hellen',56);
- insert into student values(9,'Ivan',93);
- insert into student values(10,'Jay',90);
- commit;
首先,我们来看一下UNION的例子:
- SQL> select *
- 2 from student
- 3 where id<4
- 4 union
- 5 select *
- 6 from student
- 7 where id>2 and id<6
- 8 ;
- ID NAME SCORE
- ---------- ------------------------------ ----------
- 1 Aaron 78
- 2 Bill 76
- 3 Cindy 89
- 4 Damon 90
- 5 Ella 73
- SQL>
如果换成Union All连接两个结果集,则结果如下:
- SQL> select *
- 2 from student
- 3 where id<4
- 4 union all
- 5 select *
- 6 from student
- 7 where id>2 and id<6
- 8 ;
- ID NAME SCORE
- ---------- ------------------------------ ----------
- 1 Aaron 78
- 2 Bill 76
- 3 Cindy 89
- 3 Cindy 89
- 4 Damon 90
- 5 Ella 73
- 6 rows selected.
可以看到,Union和Union All的区别之一在于对重复结果的处理。
接下来,我们交换一个两个SELECT语句的顺序,看看结果是怎样的。
- SQL> select *
- 2 from student
- 3 where id>2 and id<6
- 4 union
- 5 select *
- 6 from student
- 7 where id<4
- 8 ;
- ID NAME SCORE
- ---------- ------------------------------ ----------
- 1 Aaron 78
- 2 Bill 76
- 3 Cindy 89
- 4 Damon 90
- 5 Ella 73
- SQL> select *
- 2 from student
- 3 where id>2 and id<6
- 4 union all
- 5 select *
- 6 from student
- 7 where id<4
- 8 ;
- ID NAME SCORE
- ---------- ------------------------------ ----------
- 3 Cindy 89
- 4 Damon 90
- 5 Ella 73
- 1 Aaron 78
- 2 Bill 76
- 3 Cindy 89
- 6 rows selected.
可以看到,对于UNION来说,交换两个SELECT语句的顺序后结果仍然是一样的,这是因为UNION会自动排序。而UNION ALL在交换了SELECT语句的顺序后结果则不相同,因为UNION ALL不会对结果自动进行排序。
那么这个自动排序的规则是什么呢?我们交换一下SELECT后面选择字段的顺序(前面使用SELECT *相当于SELECT ID,NAME,SCORE),看看结果如何:
- SQL> select score,id,name
- 2 from student
- 3 where id<4
- 4 union
- 5 select score,id,name
- 6 from student
- 7 where id>2 and id<6
- 8 ;
- SCORE ID NAME
- ---------- ---------- ------------------------------
- 73 5 Ella
- 76 2 Bill
- 78 1 Aaron
- 89 3 Cindy
- 90 4 Damon
可是看到,此时是按照字段SCORE来对结果进行排序的(前面SELECT *的时候是按照ID进行排序的)。
那么有人会问,如果我想自行控制排序,能不能使用ORDER BY呢?当然可以。不过在写法上有需要注意的地方:
- select score,id,name
- from student
- where id > 2 and id < 7
- union
- select score,id,name
- from student
- where id < 4
- union
- select score,id,name
- from student
- where id > 8
- order by id desc
order by子句必须写在最后一个结果集里,并且其排序规则将改变操作后的排序结果。对于Union、Union All、Intersect、Minus都有效。
其他的集合操作符,如Intersect和Minus的操作和Union基本一致,这里一起总结一下:
Union,对两个结果集进行并集操作,不包括重复行,同时进行默认规则的排序;
Union All,对两个结果集进行并集操作,包括重复行,不进行排序;
Intersect,对两个结果集进行交集操作,不包括重复行,同时进行默认规则的排序;
Minus,对两个结果集进行差操作,不包括重复行,同时进行默认规则的排序。
可以在最后一个结果集中指定Order by子句改变排序方式。
转载于:https://www.cnblogs.com/chyg/archive/2012/11/24/2785424.html
sql中union和union all的区别相关推荐
- SQL中char varchar nchar nvarchar ntext区别和使用(资料汇总)
SQL中char varchar nchar nvarchar ntext区别和使用(资料汇总) 2008年10月14日 星期二 23:57 一.SQL中char varchar nchar nvar ...
- SQL中的or与in的区别
SQL中的or与in的区别 or与in一样的案例(条件里只有a时) a in (1,2,3) a = 1 or a = 2 or a = 3 or与in不一样的案例(条件里不止a时) where a ...
- SQL中JOIN和UNION区别及用法
转载:http://chengheng1984.blog.163.com/blog/static/17947412201012215738844/ JOIN用于按照ON条件联接两个表,主要有四种: I ...
- SQL中的cast 和convert的区别,日期操作
SQL Server 日期格式和日期操作 SQL Server发展至今, 1.关于日期的格式的控制方法, 有传统的方法,比如CONVERT(), 也有比较便利的新方法,比如FORMAT(): eg: ...
- Sql中的并(UNION)、交(INTERSECT)、差(minus)、除去(EXCEPT)详解
限制条件 (1)所有查询中的列数和列的顺序必须相同. (2)比较的两个查询结果集中的列数据类型可以不同但必须兼容. (3)比较的两个查询结果集中不能包含不可比较的数据类型(xml.text.ntext ...
- SQL中truncate table和delete的区别
truncate table table_namedelete from table_namedrop table table_name truncate table在功能上与不带where子句的de ...
- sql中in和exist语句的区别?
两者都能实现表功能查询,主要区别如下: 1.适用表的类型不同. in是子查询为驱动表,外面的表为被驱动表,故适用于子查询结果集小而外面的表结果集大的情况. exists是外面的表为驱动表,子查询里面的 ...
- SQL中NVL和NVL2有什么区别,以及NULLIF 的使用
1.NVL 格式:NVL (expr1, expr2) 含义:expr1为NULL,返回expr2:不为NULL,返回expr1. 注意两者的类型要一致 2.NVL2 格式:NVL2 (expr1, ...
- SQL中varchar和nvarchar有什么区别?
varchar(n) 长度为 n 个字节的可变长度且非 Unicode 的字符数据.n 必须是一个介于 1 和 8,000 之间的数值.存储大小为输入数据的字节的实际长度,而不是 n 个字节. nva ...
- SQL中truncate table和delete的区别 --转
内容: http://www.cnblogs.com/GT_Andy/archive/2010/01/28/1921871.html 感谢博主的分享!!! 转载于:https://www.cnblog ...
最新文章
- Prometheus监控业务指标
- 您不是订单管理的定向开发者_Web Summit 2020大会:华为在欧洲发布HMS Connect,持续助力合作伙伴与开发者的创新增长...
- php fopen 错误,php fopen函数失败怎么办
- 51nod 1096 距离之和最小 思维题,求中位数
- Eclipse常用的快捷方式
- jQuery-1.9.1源码分析系列(五) 回调对象
- echo怎么把日志清空_shell脚本清空系统message日志
- c语言自动变量与静态变量,C语言的中的静态变量和局部变量(自动变量)
- Foxconn Core Concept
- Raphael的set使用
- Matlab Tricks(十)—— padarray 的实现
- 【搜索引擎】强推!最好用资源最全的十个百度网盘搜索引擎
- 页面中文乱码,tomcat服务器,jsp乱码
- WiFi的信道与关联
- Android天天飞车游戏辅助系统
- Linguistic Regularities in Continuous Space Word Representations
- 写paper之ppt画图——不定期更新
- 阿里云云效GitCode迁移至GitLab仓库
- Win7和Win10如何使文件的视图默认按详细信息显示
- 让 AE 输出 MPEG
热门文章
- SharePoint 使用代码创建 SPWeb/SPSiite/SPWebApplication以及WebPart添加到页面与删除 (一)...
- Ubuntu14.04 桌面 launcher 终端 状态栏 失效解决
- 计划继续深化学习物联网系统的相关知识 目前的WiFi模块基于ESP8266
- 【教女朋友学网络系列3】之手把手教她明白交换机的基本原理
- oracle ajax储存过程分页,创建 Oracle 分页存储过程
- mybatis与mysql的优点_MyBatis的优缺点以及特点
- rap技术原理_「水深坑多」做分子海绵,你还需要了解这些技术
- python 用户输入_Python中如何让用户输入内容
- 问题 D: AC自动机(二分,第一个等于和最后一个等于)
- 阿里云视图计算,边缘计算的主“战”场