DQL语言学习进阶九(联合查询)
union:联合、合并,将多条查询语句的结果合并成一个结果
引例:查询部门编号>90的邮箱包含a的员工信息
1、select *from employee where email like'%a%'or department_id>10;2、select *from employee where email 立刻 '%a%'; select*from employee where department_id >90;
union
1、语法:
查询语句1 union 【all】 查询语句2 union 【all】 ……;
2、意义:
(1)将一条比较复杂的查询语句拆分成多条语句
(2)适用于查询多个表的时候,查询的列基本一致
3、应用场景:要查询的结果来自于多个表,且多个表中没有直接的连接关系,但查询的信息一致时
4、特点:
(1)要求多条查询语句的查询列数是一致的
(2)要求多条查询语句的查询的每一列的类型和顺序最好一致
(3)union关键字默认去重,用union all可以包含重复项
例:查询中国用户男性的信息和外国用户中男性的信息
select id,name,csex from t_ca where csex ='男'unionselect t_id,t_name,t_gender from t_ua where t_gender ='male';