`
sangei
  • 浏览: 330172 次
  • 性别: Icon_minigender_1
  • 来自: 西安
社区版块
存档分类
最新评论

(转)UNION and UNION ALL

阅读更多

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

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

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

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

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

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

可以在最后一个结果集中指定Order by子句改变排序方式。

例如:

Java代码 复制代码
  1. select employee_id,job_id from employees   
  2. union   
  3. select employee_id,job_id from job_history  
select employee_id,job_id from employees
union
select employee_id,job_id from job_history



以上将两个表的结果联合在一起。这两个例子会将两个select语句的结果中的重复值进行压缩,也就是结果的数据并不是两条结果的条数的和。如果希望即使重复的结果显示出来可以使用union all,例如:

2.在oracle的scott用户中有表emp

Java代码 复制代码
  1. select * from emp where deptno >= 20  
  2. union all   
  3. select * from emp where deptno <= 30  
select * from emp where deptno >= 20
union all
select * from emp where deptno <= 30

这里的结果就有很多重复值了。

有关union和union all关键字需要注意的问题是:

union 和 union all都可以将多个结果集合并,而不仅仅是两个,你可以将多个结果集串起来。
使用union和union all必须保证各个select 集合的结果有相同个数的列,并且每个列的类型是一样的。但列名则不一定需要相同,oracle会将第一个结果的列名作为结果集的列名。例如下面是一个例子:

Java代码 复制代码
  1. select empno,ename from emp   
  2. union   
  3. select deptno,dname from dept   
select empno,ename from emp
union
select deptno,dname from dept 


我们没有必要在每一个select结果集中使用order by子句来进行排序,我们可以在最后使用一条order by来对整个结果进行排序。

 

分享到:
评论

相关推荐

    UNION ALL 的使用方法

    SELECT ... UNION ALL SELECT C.TRD_NO FROM XXXXXXXXX C WHERE C.CLIENT_CD = W_IN_CLIENT_CD ) D WHERE A.TRD_NO = D.TRD_NO AND A.CLIENT_CD = W_IN_CLIENT_CD AND A.RESULT IS NULL ;

    Apache Hadoop 3.x state of the union and upgrade guidance

    Apache Hadoop YARN is the modern distributed operating system for big data applications....And you’ll leave with all the knowledge of how to upgrade painlessly from 2.x to 3.x to get all the benefits.

    获得汉字笔画

    create function fun_getbh(@char nchar(2)) returns int as begin return( case when unicode(@char) between 19968 and 40869 then( ... union all select 9,N'鸩' union all select 10,N'龀'

    5分钟了解MySQL5.7中union all用法的黑科技

    union all在MySQL5.6下的表现 Part1:MySQL5.6.25 [root@HE1 ~]# MySQL -uroot -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 2 Server version: ...

    英文原版-Bone and Joint Injuries Trauma Surgery III 3rd Edition

    There currently is a clear tendency to an increasing number of accidental injuries in elderly people, in sport injuries and car crashes also in countries which recently joined the European Union and ...

    Research on taxi settlement center system of Xi’an metropolitan union card

    at the same time, taxi settlement central itself also needs to statistic and analysis the transaction records of all the system received and then generates the trade reports,operational data report,...

    Type.Inheritance.and.Relational.Theory

    All of these topics are described in detail in the book, with numerous illustrative examples, exercises, and answers. The book also discusses several alternative approaches. In particular, it includes...

    Introduction to Information Theory and Data Compression

    The authors seem to be at home with all the types of readers, they realize that the lingo and the aim is different for the different and diverse groups of students. Indeed, the tools of information ...

    PHP+MySQL 手工注入语句大全 推荐

    暴字段长度 Order by num/* 匹配字段 and 1=1 union select 1,2,3,4,5...and 1=2 union all select version() /* and 1=2 union all select database() /* and 1=2 union all select user() /* 操作系统信息: and 1

    算法上机!!

    We highly encourage being environment friendly and trying all problems on your own. Implement exercise 2.3-7. Implement priority queue. Implement Quicksort and answer the following questions. (1) ...

    Which SQL statement is the trump card to the senior software developer

    Within so many kinds of DML statements in SQL, I think four kinds of them, including Case When, Left Outer Join/Left Join / Right Outer Join /Right Join/ Inner Join, WITH AS and UNION/UNION ALL, are ...

    oracle sql of extracting table structure

    union all select distinct lower(all_c.table_name) table_name --,(case -- when column_id || to_char(column_id) -- when column_id (column_id) -- when column_id &gt; 100 then 'H' || substrb(to_char...

    UE(官方下载)

    CSE HTML Validator for Windows is the most powerful, easy to use, user configurable, and all-in-one HTML, XHTML, CSS, link, spelling, and accessibility checker available. This quick tutorial shows you...

    SQL语句教程.doc

    SQL指令 2 SELECT 2 DISTINCT 3 WHERE 4 AND OR 5 IN 5 ...UNION ALL 37 INTERSECT 38 MINUS 39 子查询 40 EXISTS 42 CASE 43 算排名 44 算中位数 45 算总合百分比 47 算累积总合百分比 48 SQL语法 49

    Error Correction coding——mathematical methods and algorithms

    1 S.5 Bounds on Performance: The Union Bound . . . . . . . . . . . . . 22 1.5.6 The Binary Symmetric Channel . . . . . . . . . . . . . . . . . . . 23 1 S.7 The BSC and the Gaussian Channel Model . . ....

    SSD7 EX1 答案

    3. Write the SQL statement to retrieve the title and price of all books published by either of two publishers (say "Addison Wesley" and "McGraw Hill"). In the file Rel-ops.txt, list which relational ...

    PHP常见的几种攻击方式实例小结

    分享给大家供大家参考,具体如下: 1.SQL Injection(sql注入) ①.暴字段长度 Order by num/* ②.匹配字段 and 1=1 union select 1,2,3,4,5…….n/* ③.暴露字段位置 and 1=2 union select 1,...and 1=2 union all sel

    spring-framework-4.3.2.RELEASE-dist

    "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, ...

Global site tag (gtag.js) - Google Analytics