时间:2021-07-01 10:21:17 帮助过:3人阅读
1.SET运算符是什么
将多个查询用 SET 操作符连接组成一个新的查询
UNION/UNION ALL——并集
INTERSECT——交集
MINUS——差集(A\B=A中去掉B中也有的元素)
关于MySQL的交、并、差集,参考:http://blog.csdn.net/goodleiwei/article/details/42149567
2.图解
UNION取交集(自动去重,且要求两结果集列可以对应合并)
SELECT * FROM employees1
UNION
SELECT * FROM employees2
UNION ALL不去重:
SELECT * FROM employees1
UNION ALL
SELECT * FROM employees2
排序可以采用以下方式:(因为department_id是第11列,故直接写上列数即可)
SELECT * FROM employees1 UNION SELECT * FROM employees2 ORDER BY 11
也可以采取再套一层的写法,参考:https://www.2cto.com/database/201210/163404.html
当然,如果列确实不匹配,可以通过以下形式调整:
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;
SELECT * FROM employees1
INTERSECT
SELECT * FROM employees2
SELECT employee_id,job_id
FROM employees
MINUS
SELECT employee_id,job_id
FROM job_history;
Oracle入门第六天(中)——SET运算符(交并差集)
标签:img cat tab inter col 写法 span _id 通过