第十三章 SET运算符
1.SET 操作符:UNION / UNION ALL INTERSECT MINUS
2.UNION 操作符返回两个查询的结果集的并集
SELECT employee_id, job_id
FROM employees
UNION
SELECT employee_id, job_id
FROM job_history;
2.1UNION ALL 操作符:返回两个查询的结果集的并集。对于两个结果集的重复部分,不去重。
SELECT employee_id, job_id, department_id
FROM employees
UNION ALL
SELECT employee_id, job_id, department_id
FROM job_history
ORDER BY employee_id;
3.INTERSECT 操作符返回两个结果集的交集
SELECT employee_id, job_id
FROM employees
INTERSECT
SELECT employee_id, job_id
FROM job_history;
4.MINUS操作符:返回两个结果集的差集
SELECT employee_id,job_id
FROM employees
MINUS
SELECT employee_id,job_id
FROM job_history;
5.使用 SET 操作符注意事项
在SELECT 列表中的列名和表达式在数量和数据类型上要相对应
括号可以改变执行的顺序
ORDER BY 子句:
只能在语句的最后出现
可以使用第一个查询中的列名, 别名或相对位置
除 UNION ALL之外,系统会自动将重复的记录删除
系统将第一个查询的列名显示在输出中
除 UNION ALL之外,系统自动按照第一个查询中的第一个列的升序排列
6.匹配各SELECT 语句举例
SELECT department_id, TO_NUMBER(null)
location, hire_date
FROM employees
UNION
SELECT department_id, location_id, TO_DATE(null)
FROM departments;
SELECT employee_id, job_id,salary
FROM employees
UNION
SELECT employee_id, job_id,0
FROM job_history;
使用相对位置排序举例
COLUMN a_dummy NOPRINT
SELECT 'sing' AS "My dream", 3 a_dummy
FROM dual
UNION
SELECT 'I`d like to teach', 1
FROM dual
UNION
SELECT 'the world to', 2
FROM dual
ORDER BY 2;
本文出自 “阿成的博客” 博客,转载请与作者联系!