第19节--联合查询

联合查询

关键词:union 联合,合并;将多条查询语句的结果合并成一个结果

语法:查询语句1

           union

           查询语句2

           union......

应用场景:要查询的结果来自于多个表 且 多个表没有直接的连接关系,但查询的信息一致时

案例:查询部门编号>90号或邮箱中包含a的员工信息

①:以前的方式

select * from employees where department_id>90 or email like "%a%";
② 利用联合查询

select * from employees where email like "%a%"
union 
select * from employees where department_id>90;

案例、查询中国用户中男性的用户信息以及外国用户中男性的用户信息

select id, cname,csex,from t_ca where csex=""

union 

select t_id,tname, tgender from t_ua where tgender="male";

应用特点:

1. 要求多条查询的查询列表是一致的

2. 要求多条查询语句的每一列的类型和顺序最高一致

3. union 关键字默认去重,使用union all可以包含重复项

猜你喜欢

转载自www.cnblogs.com/Jasmine6-Lee/p/12699231.html
今日推荐