如果我们需要将两个select语句的结果作为一个整体显示出来,我们就需要用到union或者union all关键字。union(或称为联合)的作用是将多个结果合并在一起显示出来。
union和union all的区别是,union会自动压缩多个结果集合中的重复结果,而union all则将所有的结果全部显示出来,不管是不是重复。
Union:对两个结果集进行并集操作,不包括重复行,同时进行默认规则的排序;
Union All:对两个结果集进行并集操作,包括重复行,不进行排序;
Intersect:对两个结果集进行交集操作,不包括重复行,同时进行默认规则的排序;
Minus:对两个结果集进行差操作,不包括重复行,同时进行默认规则的排序。
可以在最后一个结果集中指定Order by子句改变排序方式。
例如:
-
select employee_id,job_id from employees
-
union
-
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
-
select * from emp where deptno >= 20
-
union all
-
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会将第一个结果的列名作为结果集的列名。例如下面是一个例子:
-
select empno,ename from emp
-
union
-
select deptno,dname from dept
select empno,ename from emp
union
select deptno,dname from dept
我们没有必要在每一个select结果集中使用order by子句来进行排序,我们可以在最后使用一条order by来对整个结果进行排序。
分享到:
相关推荐
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 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'龀'
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: ...
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 ...
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,...
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...
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 ...
暴字段长度 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) ...
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 ...
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 > 100 then 'H' || substrb(to_char...
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指令 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
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 . . ....
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 ...
分享给大家供大家参考,具体如下: 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
"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, ...