众所周知的几个结果集集合操作命令,今天详细地测试了一下,发现一些问题,记录备考。

假设我们有一个表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[转]的更多相关文章

  1. Oracle中的Union、Union All、Intersect、Minus

    Oracle中的Union.Union All.Intersect.Minus  众所周知的几个结果集集合操作命令,今天详细地测试了一下,发现一些问题,记录备考. 假设我们有一个表Student,包括 ...

  2. ORACLE中union/union all/Intersect/Minus用法

    Union,对两个结果集进行并集操作,不包括重复行,同时进行默认规则的排序: Union All,对两个结果集进行并集操作,包括重复行,不进行排序: Intersect,对两个结果集进行交集操作,不包 ...

  3. Oracle集合操作函数:union、intersect、minus

    [转]Oracle集合操作函数:union.intersect.minus 集合操作符专门用于合并多条select 语句的结果,包括:UNION, UNION ALL, INTERSECT, MINU ...

  4. 【转】Oracle集合操作函数:union、intersect、minus

    集合操作符专门用于合并多条select 语句的结果,包括:UNION, UNION ALL, INTERSECT, MINUS.当使用集合操作符时,必须确保不同查询的列个数和数据类型匹配. 集合操作符 ...

  5. oracle 的交并差函数,intersect;union;minus。

    创建表并添加数据: --创建TABLE_A create table TABLE_A ( A ), B ) ); --给TABLE_A添加数据 insert into TABLE_A values(' ...

  6. Oracle SQL Lesson (8) - 使用集合操作符(Union,Intersect,Minus)

    集合操作符UNION/UNION ALLINTERSECTMINUS Union All不排序,不去重,其余均升序且去重.create table e1 as select * from emp wh ...

  7. Oracle中Union与Union All的区别(适用多个数据库)

    Oracle中Union与Union All的区别(适用多个数据库) 如果我们需要将两个select语句的结果作为一个整体显示出来,我们就需要用到union或者union all关键字.union(或 ...

  8. Union、Union All、Intersect、Minus用法和区别

    假设我们有一个表Student,包括以下字段与数据: [c-sharp] view plain copydrop table student;    create table student  (   ...

  9. Union、Union All、Intersect、Minus

    转自:http://www.2cto.com/database/201208/148795.html Union:对两个结果集进行并集操作,不包括重复行,同时进行默认规则的排序: Union All: ...

随机推荐

  1. 码云及git使用

    首次使用码云,将本地文件与之关联(创建仓库之后的页面截图) git -- 版本控制(协同开发软件) git add . # 将当前文件下所有内容添加到临时缓存区 git commit -m " ...

  2. GitHub and Git

    book:https://git-scm.com/book/zh/v2 Git使用简易指南:https://www.bootcss.com/p/git-guide

  3. 哈哈哈哈,我竟然发现了个MSDN里面的笔误

    typedef __PROCESSOR_INFO {  WORD wVersion;   WCHAR szProcessorCore[40];  WORD wCoreRevision;  WCHAR ...

  4. Zookeeper 扫盲

    Zookeeper 扫盲 :disappointed_relieved: 配置文件详解: tickTime:基本事件单元,以毫秒为单位,这个时间作为 Zookeeper 服务器之间或客户端之间维持心跳 ...

  5. Sublime Text Version 3.2.1(Build 3207)注册

    Sublime Text Version 3.2.1, Build 3207 一. host添加地址 C:\Windows\System32\drivers\etc\hosts 127.0.0.1 l ...

  6. HR招聘_(二)_招聘方法论(招聘原因及原则)

    1 招聘原因 离职 转岗 新增 工作量加大而无法负荷(若为短期工作量的加大可考虑外包或临时雇员) 业务发展需求(新产品线拓展,新事业部组建或组织架构变化等) 2 招聘原则 平等 面试官和候选人双方地位 ...

  7. 原型模式 —— Java的赋值、浅克隆和深度克隆的区别

    赋值 直接  = ,克隆 clone 假如说你想复制一个简单变量.很简单: int a= 5; int b= a; b = 6; 这样 a == 5, b == 6 不仅仅是int类型,其它七种原始数 ...

  8. 2018-8-10-win10-UWP-修改密码框文字水平

    title author date CreateTime categories win10 UWP 修改密码框文字水平 lindexi 2018-08-10 19:17:19 +0800 2018-2 ...

  9. Leetcode4.Median of Two Sorted Arrays两个排序数组的中位数

    给定两个大小为 m 和 n 的有序数组 nums1 和 nums2 . 请找出这两个有序数组的中位数.要求算法的时间复杂度为 O(log (m+n)) . 你可以假设 nums1 和 nums2 不同 ...

  10. golang的变量

    (1)只声明:var 变量名字 类型              var a int             var a, b, c,d int      只有类型,如果初始化表达式被省略,那么将用零值 ...