~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
开发工具与关键技术:Oracle sql*plus PLSQL Developer
作者:ZCR
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
将多个查询用SET操作符连接组成一个新的查询:
Union / Union all
Intersect
Minus
Union操作符返回的是两个查询的结果并集,但不会重复
Union all 操作符返回的是两个查询的结果并集,对于两个结果集的重复部分,不去重。
Intersect 操作符返回来那个个结果的交集
Minus 返回两个结果的差值,以第一个为中心(下面有详细的例子)
下面我就一一举例说明
union
- 查询部门id20和部门id30的员工信息
select * from employees
where department_id = '20'
union
select * from employees
where department_id = '30'
union all :和union的区别是,union是可以去重复的,union all相同的也不去重复
2. 查询工资3500和部门3000到4000的员工信息
SELECT *
FROM employees
where salary = '3500'
UNION all
SELECT *
FROM employees
where salary between 3500 and 4000
Intersect:返回的是共同的部分数据
3.查询部门id为100和部门id为60的相同工资
select salary from employees where department_id = 100
Intersect
select salary from employees where department_id = 60
Minus:返回的是第一个数据集(与第二个数据集不相同的数据)的数据
解析: (
A(1,2,3,6)
Minus
B(2,3,8,9)
返回的是(1,6)
)
而并不会返回8,9,自返回以第一个以第一个数据集为中心不同的数据
4.查询部门id为100和部门id为60的不相同工资
select salary from employees where department_id = 100
minus
select salary from employees where department_id = 60
第3,4题数据的查询结果
- 部门id为100的工资
- 部门id为60的工资