- union:将多条查询语句的结果合并成一个结果
- 语法:
查询语句1
union
查询语句2
union
...
- 举例:查询部门编号>90或者邮箱包含a的员工信息
select * from employees
where department_id>90 or email like '%a%';
# 使用union
select * from employees
where department_id >90
union
select * from employees
where email like '%a%';
- 应用场景:要查询的结果来自于多个表,且多个表没有直接的连接关系,但需要查询的信息一致时
- 需要注意的问题:
1.要求多条查询语句的查询列数是一致的
2.要求多条查询语句的查询的每一列的类型和顺序最好一致
3.union关键字默认去重,如果使用union all可以包含重复项 - 例子:
select id, cname from t_ca where csex = '男'
union
select t_name, t_id from t_ua where tGender = 'male';