2021.10.31
set操作符
union
取并集,union仅包含一个表中的重复值,union all包含两表中的重复值
select employee_id emp_id, department_id dept_id
--可以在第一个表的列中写别名,第一个表中写了就不用写在第二个表中
from employees01
union
select employee_id,department_id
--两表内容需要一一对应
from employees02
order by emp_id
intersect
取交集
minus
取差集
相对位置
可以使用相对位置进行排序
select employee_id emp_id, department_id dept_id
--可以在第一个表的列中写别名,第一个表中写了就不用写在第二个表中
from employees01
union
select employee_id,department_id
--两表内容需要一一对应
from employees02
order by 1 --根据第一个列进行排序,即employee_id
排序顺序使用
select 'a' as 'column_name' from dual
union
select 'b' from dual
union
select 'c' from dual
会导致结果为bca
需要添加序号进行排序
select 'a' as 'column_name' ,1 a_dummy from dual
union
select 'b' ,2 from dual
union
select 'c' ,3 from dual
order by 2 asc--使用第二列进行排序
no print
添加SQL语句column a_dummy noprint
在命名为a_dummy后该列不会显示
确保列一一对应
添加null列保证能够一一对应
select employee_id emp_id,department_id,to_char(null)
from employees01
union
select to_number(null),department_id,department_name
from departments