联合查询 - union,union all
对于union查询,就是把多次查询的结果合并起来,形成一个新的查询结果集
SELECT 字段列表 FROM 表A ... UNION [ALL] SELECT 字段列表 FROM 表B ...;
1.将薪资低于5000的员工,和 年龄大于50的员工全部查询出来
use itcast;
select *
from participators
where salary < 5000
union all
select *
from participators
where age > 50;
select *
from participators
where salary < 5000
union
select *
from participators
where age > 50;
select *
from participators
where salary < 5000
union
select name
from participators
where age > 50;
# [21000][1222] The used SELECT statements have a different number of columns
对于联合查询的多张表的列数必须保持一致,字段类型也要保持一致
union all会将全部的数据直接合并在一起,而union会进行去重操作