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;

posted @   HIK4RU44  阅读(64)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 一文读懂知识蒸馏
· 终于写完轮子一部分:tcp代理 了,记录一下
点击右上角即可分享
微信分享提示