Union和Union All的差别
如果我们有一个表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子句改变排序方式。
Union和Union All的差别的更多相关文章
- mysql union和union all 的差别以及使用
Union由于要进行反复值扫描,所以效率低.假设合并没有刻意要删除反复行,那么就使用Union All 两个要联合的SQL语句 字段个数必须一样.并且字段类型要"相容"(一致). ...
- union和union all用法
工作中,遇到同事之前写的oracle语句中有一个union all,并且很多地方都用到了.便在网上查了一下用法,以下是自己的理解. union (联合)将两个或者多个结果集合并. 在使用时,两个结果 ...
- mysql中union与union all的区别
当查询表结构完全相同的多张表的数据时: 1.当查询条件完全相同且不包括主键,此时用union查询会过滤掉查询出的重复的记录,及漏查记录:使用union all进行查询,则会查出所有的符合条件的记录,保 ...
- 【转载】SQL语句中Union和Union All的区别
在使用到SQL语句进行数据库查询的过程中,如果需要求两个数据集合的并集,一般会使用到联合查询关键字Union或者Union All,其实Union和Union All两者的使用有一定差别,查出来的数据 ...
- 【oracle】union、union all、intersect、minus 的用法及区别
一.union与union all 首先建两个view create or replace view test_view_1 as as c from dual union as c from dua ...
- Oracle 中 union 和union all 的简单使用说明
1.刚刚工作不久,经常接触oracle,但是对oracle很多东西都不是很熟.今天我们来了解一下union和union all的简单使用说明.Union(union all): 指令的目的是将两个 S ...
- 【转】Mysql联合查询union和union all的使用介绍
Mysql的联合查询命令UNION和UNION ALL,总结了使用语法和注意事项,以及学习例子和项目例子,需要的朋友可以参考下 一.UNION和UNION ALL的作用和语法 UNION 用于合... ...
- SQL UNION 和 UNION ALL 操作符\SQL SELECT INTO 语句\SQL CREATE DATABASE 语句
SQL UNION 操作符 UNION 操作符用于合并两个或多个 SELECT 语句的结果集. 请注意,UNION 内部的 SELECT 语句必须拥有相同数量的列.列也必须拥有相似的数据类型.同时,每 ...
- Union和Union All到底有什么区别
以前一直不知道Union和Union All到底有什么区别,今天来好好的研究一下,网上查到的结果是下面这个样子,可是还是不是很理解,下面将自己亲自验证: Union:对两个结果集进行并集操作,不包括重 ...
随机推荐
- [逼死强迫症 - C&C++设计风格选择.1] : 命名规范
1.命名规范 本系列的第一篇,命名风格本就是有关艺术审美,没有美与丑的绝对标准,本文难免带有主观选择倾向,但是会尽量保持客观的态度归纳几种主流的命名风格,仅供参考.制定规范是为了方便团队沟通和利于代码 ...
- 8.模板方法模式-[Head First 设计模式]
模板方法模式在一个方法中定义一个算法的骨架,而将一些步骤延迟到子类中.模板方法使得子类可以在不改变算法结构的情况下,重新定义算法中的某些步骤. 要点: “模板方法”定义了算法的步骤,把这些步骤的实现延 ...
- rhel安装eclipse
smb --> IDE --> 环境gcc(开发c) g++(开发c++)c++操作linux --> sqlite数据库linux平台自带sqlite数据库 基本SQL语言划分:D ...
- 2D简单图形相关算法罗列
因为平常在Qt开发过程中经常会与一些简单的2D几何图形打交道,因此学习和掌握一些基本的2D几何计算还是很有必要的,在这里罗列一些常用的基本情况,之后会适时补充. [1] 两点之间距离,根据两个点的差值 ...
- Spring3.0提供的表达式语言spel
package com.zf.spel; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.D ...
- c#中使用easyUI的DataGrid组件
前台页面 html <table id="dg"> </table> JavaScript $("#dg").datagrid({ wi ...
- Automatically watermark all uploaded photos (给所有上传的相片加水印)
Hello, This mod automatically watermark all uploaded photos. Price: FREE, enjoy. You will have to ed ...
- Python自动化运维之17、Python操作 Memcache、Redis、RabbitMQ
一.Memcached Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态.数据库驱动网站的 ...
- 在sublimetext上打造一个兼容virtualenv的web&python开发环境
利用Sublimetext3&virtualenv 打造一个Web&Python IDE 注: 环境:window|python3;以下使用的sublimetext插件均用packag ...
- Linux shell (一)
echo -e "Hello World! \a \n" # -e 解析反斜杠 read -p "Please input your first name: &q ...