Oracle Database之集合操作函数(UNION, INTERSECT, MINUS)

Oracle Database之集合操作属于SELECT的高级用法,集合操作包含并、交、差三种,为了合并多个SELECT语句的结果,可以使用的集合操作符: UNION, UNION ALL, INTERSECT和MINUS.


1.语法

SELECT statement1
[UNION | UNION ALL | INTERSECT | MINUS]
SELECT statement2
...


创建表并插入样例数据:

--创建表employee
create table employee
(
  employee_id number,
  employee_name varchar2(100),
  employee_gender varchar2(1),
  hire_date date,
  job_type varchar2(100),
  department_id number,
  salary number(22,2),
  bonus number(22,2)
);
--插入样例数据:
insert into employee values(1, 'david','M',sysdate-1800,'Database', 10,2000, 2000);
insert into employee values(2, 'black','M',sysdate-1000,'Database',10, 2600, 2000);
insert into employee values(3, 'josen','M',sysdate-2000,'Jira',12, 2700, 2000);
insert into employee values(4, 'klaus','M',sysdate-1020,'System',13, 2800, 2000);
insert into employee values(5, 'fab','M',sysdate-365,'Java', 11,2200, 2900);
insert into employee values(6, 'derek','M',sysdate-1600,'IT',14, 2600, 2400);

--创建表: employee_history
create table employee_history
(
  employee_id number,
  employee_name varchar2(100),
  employee_gender varchar2(1),
  hire_date date,
  job_type varchar2(100),
  department_id number,
  salary number(22,2),
  bonus number(22,2)
);

--插入样例数据:
insert into employee_history values(1, 'david','M',sysdate-1800,'Database', 10,2000, 2000);
insert into employee_history values(2, 'black','M',sysdate-1000,'Database', 10,2600, 2300);
insert into employee_history values(3, 'kaishen','M',sysdate-2200,'Java', 11,4000, 2500);
insert into employee_history values(4, 'arvin','M',sysdate-365,'System',11, 6000, 2100);
insert into employee_history values(5, 'light','M',sysdate-650,'Java',11, 8000, 1000);
insert into employee_history values(6, 'derek','M',sysdate-1600,'IT', 14,2000, 2000);

通过样例数据可以看出,两个表中部分数据是相同的。
2. UNION 和UNION ALL

UNION操作符用于获取两个或者多个结果集的并集,当使用该操作符时,会自动去掉结果集中的重复行。

UNION ALL操作符返回查询所检索出的所有行,包括重复的行;

示例1: 查询出表employee和employee_history表中所有的员工姓名、性别、工作类型,并根据姓名排序(备注:包含重复数据)

select employee_name, employee_gender, job_type from employee
union all
select employee_name, employee_gender, job_type from employee_history
order by employee_name asc;
Oracle Database之集合操作函数(UNION, INTERSECT, MINUS)_第1张图片
从上图可以看出,查询结果为两个查询的并集,包含了重复数据。


示例2: 查询出表employee和employee_history表中所有的员工姓名、性别、工作类型,并根据姓名排序(备注:去除重复数据)

select employee_name, employee_gender, job_type from employee
union
select employee_name, employee_gender, job_type from employee_history
order by employee_name asc;
Oracle Database之集合操作函数(UNION, INTERSECT, MINUS)_第2张图片
从上图可以看出,查询结果为两个查询的并集,并去除了重复数据。


备注: 如果使用ORDER BY子句进行排序,该子句只出现在最后一个查询的后面。


3. INTERSECT

INTERSECT操作符用于获取两个结果集的交集。当时用该操作符时,只会显示同时存在两个结果集中的数据。

示例:

select employee_name, employee_gender, job_type from employee
intersect
select employee_name, employee_gender, job_type from employee_history


4. MINUS

MINUS操作符用于获取两个结果集的差集。使用该操作符,只会显示第一个结果集中存在,第二个结果集中不存在的数据。

select employee_name, employee_gender, job_type from employee
minus
select employee_name, employee_gender, job_type from employee_history



-------------------------------------------------------

本文系原创,转载请表明出处!

你可能感兴趣的:(------,Oracle)