联合结果集union
简单的结果集联合:
select number,name,age from emp
union
select cardnumber,name,age from emp2
基本的原则:每个结果集必须有相同的列数,每个结果集的列必须类型相容。
select number,name,age,dept from emp
union
select cardnumber,name,age,'temp' from emp2
union因为要进行重复值扫描,所以效率低,因此如果不是确定要合并重复行,那么就用union all。
案例1:
要求查询员工的最低年龄和最高年龄,临时工和正式员工要分别查询
select '正式员工最高年龄', max(age) from emp
union all
select '正式员工最高年龄', min(age) from emp
union all
select '临时员工最高年龄', max(age) from emp
union all
select '临时员工最高年龄', min(age) from emp
案例二
查询每位正式员工的信息,包括工号,工资,并且在最后一行加上所有员工工资额合计
select number,salary from emp
union
select '工资合计',sum(salary) from emp