Oracle中的Union、Union All、Intersect、Minus[转]
众所周知的几个结果集集合操作命令,今天详细地测试了一下,发现一些问题,记录备考。
假设我们有一个表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和Union All的区别。
select * from student where id < 4
union
select * from student where id > 2 and id < 6
结果将是
1 Aaron 78 2 Bill 76 3 Cindy 89 4 Damon 90 5 Ella 73
如果换成Union All连接两个结果集,则返回结果是:
1 Aaron 78 2 Bill 76 3 Cindy 89 3 Cindy 89 4 Damon 90 5 Ella 73
可以看到,Union和Union All的区别之一在于对重复结果的处理。
接下来我们将两个子查询的顺序调整一下,改为
--Union
select * from student where id > 2 and id < 6
union
select * from student where id < 4
看看执行结果是否和你期望的一致?
--Union All
select * from student where id > 2 and id < 6
union all
select * from student where id < 4
那么这个呢?
据此我们可知,区别之二在于对排序的处理。Union All将按照关联的次序组织数据,而Union将进行依据一定规则进行排序。那么这个规则是?我们换个查询方式看看:
select score,id,name from student where id > 2 and id < 6
union
select score,id,name from student where id < 4
结果如下:
73 5 Ella 76 2 Bill 78 1 Aaron 89 3 Cindy 90 4 Damon
和我们预料的一致:将会按照字段的顺序进行排序。之前我们的查询是基于id,name,score的字段顺序,那么结果集将按照id优先进行排序;而现在新的字段顺序也改变了查询结果的排序。并且,是按照给定字段a,b,c...的顺序进行的order by。即结果是order by a,b,c...........的。我们看下一个查询:
select score,id,name from student where id > 2
union
select score,id,name from student where id < 4
结果如下:
56 8 Hellen 61 6 Frado 73 5 Ella 76 2 Bill 78 1 Aaron 89 3 Cindy 90 4 Damon 90 10 Jay 93 9 Ivan 99 7 Gill
可以看到,对于score相同的记录,将按照下一个字段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子句改变排序方式
Oracle中的Union、Union All、Intersect、Minus[转]的更多相关文章
- Oracle中的Union、Union All、Intersect、Minus
Oracle中的Union.Union All.Intersect.Minus 众所周知的几个结果集集合操作命令,今天详细地测试了一下,发现一些问题,记录备考. 假设我们有一个表Student,包括 ...
- ORACLE中union/union all/Intersect/Minus用法
Union,对两个结果集进行并集操作,不包括重复行,同时进行默认规则的排序: Union All,对两个结果集进行并集操作,包括重复行,不进行排序: Intersect,对两个结果集进行交集操作,不包 ...
- Oracle集合操作函数:union、intersect、minus
[转]Oracle集合操作函数:union.intersect.minus 集合操作符专门用于合并多条select 语句的结果,包括:UNION, UNION ALL, INTERSECT, MINU ...
- 【转】Oracle集合操作函数:union、intersect、minus
集合操作符专门用于合并多条select 语句的结果,包括:UNION, UNION ALL, INTERSECT, MINUS.当使用集合操作符时,必须确保不同查询的列个数和数据类型匹配. 集合操作符 ...
- oracle 的交并差函数,intersect;union;minus。
创建表并添加数据: --创建TABLE_A create table TABLE_A ( A ), B ) ); --给TABLE_A添加数据 insert into TABLE_A values(' ...
- Oracle SQL Lesson (8) - 使用集合操作符(Union,Intersect,Minus)
集合操作符UNION/UNION ALLINTERSECTMINUS Union All不排序,不去重,其余均升序且去重.create table e1 as select * from emp wh ...
- Oracle中Union与Union All的区别(适用多个数据库)
Oracle中Union与Union All的区别(适用多个数据库) 如果我们需要将两个select语句的结果作为一个整体显示出来,我们就需要用到union或者union all关键字.union(或 ...
- Union、Union All、Intersect、Minus用法和区别
假设我们有一个表Student,包括以下字段与数据: [c-sharp] view plain copydrop table student; create table student ( ...
- Union、Union All、Intersect、Minus
转自:http://www.2cto.com/database/201208/148795.html Union:对两个结果集进行并集操作,不包括重复行,同时进行默认规则的排序: Union All: ...
随机推荐
- String--在内存中的表现
创建字符串的方法有两种: Stringstr1=”直接赋值法” Stringstr2=new String(“通过new关键字的方法来创建”); 在执行String str1=” ...
- idea中HTML格式化时标签缩进问题
在IntelliJ Idea中HTML格式化时,默认<head><body>以及<body>下的以及标签都不会缩进. 解决方法:editor->code st ...
- Mac OS 安装 独立的asio库
先安装boost,见前文,然后上官网下载不带boost的asio,版本为:asio-1.12.2 cd到下载的库目录,配置 ./configure --with-boost="boost的安 ...
- python基础--迭代器、生成器、内置函数、面向对象编程
迭代器:迭代器对象从集合的第一个元素开始访问,直到所有的元素都被访问完结束.迭代器只能往前不会后退 迭代:更新换代(重复)的过程,每次的迭代都必须基于上一次的结果 迭代器:迭代取值的工具 使用迭代器的 ...
- Leetcode657.Robot Return to Origin机器人能否返回原点
在二维平面上,有一个机器人从原点 (0, 0) 开始.给出它的移动顺序,判断这个机器人在完成移动后是否在 (0, 0) 处结束. 移动顺序由字符串表示.字符 move[i] 表示其第 i 次移动.机器 ...
- 考试总结 模拟28(W)
心得: 状态极差,都怪放假,上一套的T3没改完,今天考试没有一点状态,开学恐惧症.(不恐惧作业或一调但还是很茫然) T1能A掉实在是意外,杂题T1没做过,可能是人品守恒,(丢了钱今天才发现以后一定锁柜 ...
- QT_string转char*
char* convertQString2char(const QString &str) { QByteArray ba = str.toUtf8(); char * pathChar = ...
- Linux安装MariaDB(Mysql)和简单配置 mariadb
Linux安装MariaDB(Mysql)和简单配置 1.安装MariaDB 安装命令 yum -y install mariadb mariadb-server 安装完成MariaDB,首先启动Ma ...
- Android消息机制使用注意事项,防止泄漏
在Android的线程通信当中,使用频率最多的就是Android的消息处理机制(Handler.send().View.post().Asynctask.excute()等等都使用到了消息处理机制). ...
- Codeforces Round #263 (Div. 2) A. Appleman and Easy Task【地图型搜索/判断一个点四周‘o’的个数的奇偶】
A. Appleman and Easy Task time limit per test 1 second memory limit per test 256 megabytes input sta ...