如果我们需要将两个select语句的结果作为一个整体显示出来,我们就需要用到union或者union all关键字。union(或称为联合)的作用是将多个结果合并在一起显示出来。

union和union all的区别是,union会自动压缩多个结果集合中的重复结果,而union all则将所有的结果全部显示出来,不管是不是重复。

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

Union all:对两个结果集进行并集操作,包括重复行,不进行排序;

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

Minus:对两个结果集进行差操作,不包括重复行,同时进行默认规则的排序。

下面以实例说明Union与Union all的区别:

1、首先创建一张jack表:

SQL> create table jack
2 (
3 id int primary key,
4 name varchar2(30) not null,
5 score number not null
6 ); 表已创建。
QL> insert into jack values(1,'Aaron',78);
SQL> insert into jack values(2,'Bill',76);
SQL> insert into jack values(3,'Cindy',89);
SQL> insert into jack values(4,'Damon',90);
SQL> insert into jack values(5,'Ella',73);
SQL> insert into jack values(6,'Frado',61);
SQL> insert into jack values(7,'Gill',99);
SQL> insert into jack values(8,'Hellen',56);
SQL> insert into jack values(9,'Ivan',93);
SQL> insert into jack values(10,'Jay',90);
SQL> commit; SQL> select * from jack; ID NAME SCORE
---------- -------------------- ----------
1 Aaron 78
2 Bill 76
3 Cindy 89
4 Damon 90
5 Ella 73
6 Frado 61
7 Gill 99
8 Hellen 56
9 Ivan 93
10 Jay 90 已选择10行。

2、使用union与union all进行查询:

SQL> select * from jack where id<4
2 union
3 select * from jack where id >2 and id<6; ID NAME SCORE
---------- -------------------- ----------
1 Aaron 78
2 Bill 76
3 Cindy 89
4 Damon 90
5 Ella 73 SQL> select * from jack where id<4
2 union all
3 select * from jack where id >2 and id<6; ID NAME SCORE
---------- -------------------- ----------
1 Aaron 78
2 Bill 76
3 Cindy 89
3 Cindy 89
4 Damon 90
5 Ella 73 已选择6行。

从上面的两个查询中可以看出它们的区别之一在于对重复结果的处理。
3、调整两个子查的顺序:

SQL> select * from jack where id >2 and id<6
2 union
3 select * from jack where id<4 ; ID NAME SCORE
---------- -------------------- ----------
1 Aaron 78
2 Bill 76
3 Cindy 89
4 Damon 90
5 Ella 73 SQL> select * from jack where id >2 and id<6
2 union all
3 select * from jack where id<4 ; ID NAME SCORE
---------- -------------------- ----------
3 Cindy 89
4 Damon 90
5 Ella 73
1 Aaron 78
2 Bill 76
3 Cindy 89 已选择6行。

它们两者的区别之二在于对排序的处理。Union all将按照关联的次序组织数据,而Union将进行依据一定规则进行排序。
4、验证Union排序规则(调整一下查询字段的顺序):

SQL> select score,id,name from jack where id >2 and id<6
2 union
3 select score,id,name from jack where id<4 ; SCORE ID NAME
---------- ---------- --------------------
73 5 Ella
76 2 Bill
78 1 Aaron
89 3 Cindy
90 4 Damon

可以看到之前的查询是基于id,name,score的字段顺序,那么结果集将按照id优先进行排序;而现在新的字段顺序也改变了产讯结果的排序,由此可以按照union的排序是按照第一个字段进行排序的,但是我们也可以进行干预,指定按某个字段进行排序。
5、指定某个字段进行排序

SQL> select * from
2 (
3 select score,id,name from jack where id>2 and id<7
4 union
5 select score,id,name from jack where id<4
6 union
7 select score,id,name from jack where id>8
8 )
9 order by id desc; SCORE ID NAME
---------- ---------- --------------------
90 10 Jay
93 9 Ivan
61 6 Frado
73 5 Ella
90 4 Damon
89 3 Cindy
76 2 Bill
78 1 Aaron 已选择8行。

Oracle之Union与Union all的区别的更多相关文章

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

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

  2. Oracle中 union 和 union all 的区别

    如果我们需要将两个select语句的结果作为一个整体显示出来,我们就需要用到union或者union all关键字. union(或称为联合)的作用是将多个结果合并在一起显示出来. union和uni ...

  3. oracle中union和union all区别与性能分析

    [ 概要 ] 经常写sql的同学可能会用到union和union all这两个关键词, 可能你知道使用它们可以将两个查询的结果集进行合并, 那么二者有什么区别呢? 下面我们就简单的分析下. [ 比较 ...

  4. 【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 ...

  5. Oracle 中 union 和union all 的简单使用说明

    1.刚刚工作不久,经常接触oracle,但是对oracle很多东西都不是很熟.今天我们来了解一下union和union all的简单使用说明.Union(union all): 指令的目的是将两个 S ...

  6. union与union all 的区别

    Union与Union All的区别 如果我们需要将两个select语句的结果作为一个整体显示出来,我们就需要用到union或者union all关键字.union(或称为联合)的作用是将多个结果合并 ...

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

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

  8. SQL Union 和Union All 的区别

    Union与Union All的区别 如果我们需要将两个select语句的结果作为一个整体显示出来,我们就需要用到union或者union all关键字.union(或称为联合)的作用是将多个结果合并 ...

  9. sql中UNION和UNION ALL的区别

    写sql时我们经常会遇到需要把从多张表查询的集果集进行合并.这时就用到了union.使用union或union all 时一定要保证查询的列的一致性 .不然sql会报错.字段不一致的话可以用单引号来占 ...

随机推荐

  1. Android --Android Stuido混淆签名打包

    参考博客:Android studio 使用心得(五)—代码混淆和破解apk 参考博客:Android studio 使用心得(四)---android studio 多渠道打包 参考博客:Andro ...

  2. linux内核编译相关

    参考:http://www.arm.linux.org.uk/docs/kerncomp.php 一. 内核编译1) linux 2.4make clean/make mrpropermake dep ...

  3. int[] List<int> 排序

    ; List<,,,,,,}; ,,,,}; List<int> result = allingInts.ToList(); result.Sort(); allingInts = ...

  4. [GeoServer]重拾GeoServer之安装篇

    GeoServer的项目是一个完整的Java(J2EE)系统,现实了OpenGIS联盟的网络功能服务器规范和网络覆盖服务器规范,并且集成了Web地图服务器. 在大三的时候WebGIS课程中老师讲解过一 ...

  5. How to use JDBC-Authentication of Spring Boot/Spring Security with Flyway

    java.lang.IllegalStateException: Failed to load ApplicationContext at org.springframework.test.conte ...

  6. spring 定时任务标注

    使用spring框架,需要定时任务只需要在方法上加@Component 就可以了 package hello; import java.text.SimpleDateFormat; import ja ...

  7. 一个NULL引发的血案

    go sql.stmt query 发生了一个NULL值,所以发现了error, 发现服务不停的初始化sql stmt, 导致连接数过多,服务就变得很慢. 首先,我在初始化的之前,要判断这个是否是NU ...

  8. struts标签小记

    1.<s:iterator>标签的  奇偶数行使用不同样式 <s:iterator id="list" value="#request.listq&qu ...

  9. weka特征选择(IG、chi-square)

    一.说明 IG是information gain 的缩写,中文名称是信息增益,是选择特征的一个很有效的方法(特别是在使用svm分类时).这里不做详细介绍,有兴趣的可以googling一下. chi-s ...

  10. 《30天自制操作系统》04_day_学习笔记

    harib01a: P65 用C语言实现内存写入 实现一个往黑画面上写入东西的函数 修改了naskfunc.nas中的内容 在bootpack.c中 用write_mem8()函数将VRMA中全部写入 ...