首页 > 其他 > 详细

5.9 进阶9:联合查询

时间:2021-05-29 17:31:21      阅读:15      评论:0      收藏:0      [点我收藏+]

5.9 进阶9:联合查询

5.9.1 联合查询定义

  • union联合 合并:将多条查询语句的结果合并成一个结果

5.9.2 语法

select 字段|常量|表达式|函数 【from 表】 【where 条件】 union 【all】
select 字段|常量|表达式|函数 【from 表】 【where 条件】 union 【all】
select 字段|常量|表达式|函数 【from 表】 【where 条件】 union  【all】
.....
select 字段|常量|表达式|函数 【from 表】 【where 条件】

5.9.3 特点

  • 当查询的结果来自于多个表i,且多个表没有直接的连接关系,当查询的信息一致时
    • 多条查询语句的查询的列数必须是一致的
    • 多条查询语句的查询的列的类型几乎相同
  • union查询的结果会自动去重,union all代表不去重

5.9.4 案例

  • 查询部门编号>90或邮箱包含a的员工信息
select * from employees where email like ‘%a%‘ or department_id>90;

select * from employees where email like ‘%a%‘
union
select * from employees where department_>90;
  • 查询中国用户年龄男性的信息以及外国用户男性的用户信息
select id,cname,csex,t_ca from t_ca where csex=‘男‘
union
select t_id,tname,tgender from t_us where tgender=‘male‘

5.9 进阶9:联合查询

原文:https://www.cnblogs.com/qifanren/p/14825038.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!