MYSQL学习笔记24: 多表查询(联合查询,Union, Union All)
多表查询(联合查询,union,union all)
-
union查询需要多张表的列数一致, 字段类型也保持一致
-
对于union查询, 就是把多次查询的结果合并起来, 形成一个新的查询结果集
select 字段列表 from 表A ...
union [all]
select 字段列表 from 表B ...;
查询出薪资低于10000,或年龄大于30的员工
union all
select * from emp e1 where salary<10000
union all
select * from emp e2 where age>30 order by id;
line2和line3 因为同时满足 salary<10000和age>30的条件,出现了两次,去掉all关键字就可以去重

union
select * from emp e1 where salary<10000
union
select * from emp e2 where age>30 order by id;


浙公网安备 33010602011771号