五、多表查询-3.4连接查询-联合查询union

一、概述

二、演示

【例】将薪资低于5000的员工,和 年龄大于50岁的 员工全部查询出来

1、查询薪资低于5000的员工

2、查询年龄大于50岁的员工

3、将薪资低于5000的员工,和 年龄大于50岁的 员工全部查询出来(把上面两部分的结果集直接合并起来)

4、去重,"鹿杖客"既薪资低于5000,年龄也大于50岁,所以有两条数据

union all 改为 union,即可去重

5、union查询的条件:字段要相同

如下:*有8个字段,那么只有1个字段,无法用union查询

【代码】

sql 复制代码
-- --------------------------------多表查询--------------------------------
-- 准备数据
create table dept1(
    id int auto_increment comment 'ID' primary key ,
    name varchar(50) not null comment '部门名称'
) comment '部门表';
insert into dept1 (id, name) values (1, '研发部'), (2, '市场部'), (3, '财务部'), (4, '销售部'), (5, '总经办'), (6, '人事部');

create table emp1(
    id int auto_increment comment 'ID' primary key,
    name varchar(50) not null comment '姓名',
    age int comment '年龄',
    job varchar(20) comment '职位',
    salary int comment '薪资',
    entrydate date comment '入职时间',
    managerid int comment '直属领导ID',
    dept_id int comment '部门ID'
) comment '员工表';
insert into emp1(id, name, age, job, salary, entrydate, managerid, dept_id)
values (1, '金庸', 66, '总裁', 20000, '2000-01-01', null, 5),
       (2, '张无忌', 20, '项目经理', 12500, '2005-12-05', 1, 1),
       (3, '杨逍', 33, '开发', 8400, '2000-11-03', 2, 1),
       (4, '韦一笑', 48, '开发', 11000, '2002-02-05', 2, 1),
       (5, '常遇春', 43, '开发', 10500, '2004-09-07', 3, 1),
       (6, '小昭', 19, '程序员鼓励师', 6600, '2004-10-12', 2, 1),
       (7, '灭绝', 60, '财务总监', 8500, '2002-11-12', 1, 3),
       (8, '周芷若', 19, '会计', 48000, '2006-8-12', 7, 3),
       (9, '丁敏君', 23, '出纳', 5250, '2009-1-12', 7, 3),
       (10, '赵敏', 20, '市场部总监', 12500, '2004-4-12', 1, 2),
       (11, '鹿掌客', 56, '职员', 37500, '2001-3-12', 10, 2),
       (12, '鹤壁翁', 19, '职员', 3750, '2002-10-12', 10, 2),
       (13, '东方白', 19, '职员', 5550, '2003-12-12', 10, 2),
       (14, '陆丰', 88, '销售总监', 14000, '2004-8-12', 1, 4),
       (15, '俞连州', 38, '销售', 4600, '2005-7-12', 14, 4),
       (16, '宋院桥', 40, '销售', 4600, '2008-9-12', 14, 4),
       (17, '陈友谅', 42, null, 2000, '2001-10-12', 1, null);
alter table emp1 add constraint fk_emp1_dept_id foreign key (dept_id) references dept1(id);

-- 多表查询  -- 消除无效的笛卡尔积
select * from emp1, dept1 where dept_id = dept1.id;

-- 内连接演示
-- 1、查询每一个员工的姓名, 及关联的部门的名称(隐式内连接实现)
-- 表结构:员工表emp1表,部门表dept1表
-- 连接条件:emp1.dept_id = dept1.id
--   step1.查询所有
select * from emp1, dept1 where emp1.dept_id = dept1.id;
--   step2.查询员工姓名、部门名称
select emp1.name, dept1.name from emp1, dept1 where emp1.dept_id = dept1.id;
--   step3.防止表名称过长,给表起别名
select * from emp1 e, dept1 d where e.dept_id = d.id;
--   step4.别名,查询员工姓名、部门名称
select e.name, d.name from emp1 e, dept1 d where e.dept_id = d.id;
--   step5.起别名后,还能用原表名吗?------不能
select e.name, d.name from emp1 e, dept1 d where emp1.dept_id = dept1.id;
-- 2、查询每一个员工的姓名, 及关联的部门的名称(显式内连接实现)  inner join...on...
select e.name, d.name from emp1 e inner join  dept1 d on e.dept_id = d.id;
select e.name, d.name from emp1 e join  dept1 d on e.dept_id = d.id;

-- 外连接演示
-- 1、查询emp1表的所有数据(17条),和对应的部门信息(左外连接)      内连接只能查到16条数据,dept_id为null的数据查不出来,左外连接可以查到所有17条数据
--    表结构:员工表emp1表,部门表dept1表
--    连接条件:emp1.dept_id = dept1.id
select e.*, d.name from emp1 e left outer join dept1 d on e.dept_id = d.id;
select e.*, d.name from emp1 e left  join dept1 d on e.dept_id = d.id;
-- 2、查询dept1表的所有数据,和对应的员工信息(右外连接)
select d.*, e.* from emp1 e right outer join dept1 d on e.dept_id = d.id;
-- 3、右外连接 改为 左外连接
select d.*, e.* from dept1 d left outer join emp1 e on e.dept_id = d.id;

-- 自连接演示
-- 1、查询员工 及其 所属领导的名字(managerid,领导也是员工表emp1表中的数据)!!必须起别名!! ------内连接只查询交集部分的数据
--    表结构:emp1
--    连接条件:emp1.managerid = emp1.id
select a.name, b.name from emp1 a, emp1 b where a.managerid = b.id;

-- 2、查询所有员工emp1 及其领导的名字emp1, 如果员工没有领导,也需要查询出来  ------外连接包含左表或右表
select a.name '员工', b.name '领导' from emp1 a left join emp1 b on a.managerid = b.id;


-- union all, union
-- 1、将薪资低于5000的员工, 和 年龄大于50岁的 员工全部查询出来
select * from emp1 where salary < 5000
union
select * from emp1 where age > 50;
相关推荐
点灯小铭1 分钟前
基于51单片机的LED点阵汉字显示系统设计
数据库·单片机·嵌入式硬件·毕业设计·51单片机·课程设计·期末大作业
SilentSamsara3 分钟前
DuckDB + Python:嵌入式 OLAP 数据库的轻量分析实战
开发语言·数据库·python·微服务
Nturmoils9 分钟前
从 MySQL 到 KingbaseES:Database、Schema、User 一次讲透
数据库·后端
我是一颗柠檬9 分钟前
【Redis】Redis面试高频考点汇总Day15(2026年)
数据库·redis·缓存·面试
Amnesia0_016 分钟前
MYSQL中表的基本查询
数据库·mysql
Database_Cool_18 分钟前
Doris vs 阿里云 AnalyticDB MySQL vs ClickHouse:3 大 OLAP 产品 2026 深度对比
数据库·mysql·阿里云
金融支付架构实战指南20 分钟前
秒杀&支付订单异步落地|Redis Stream 可靠队列实战
数据库·redis·缓存·stream·秒杀
小二·21 分钟前
AI Agent 数据库运维实战
运维·数据库·人工智能
曹牧21 分钟前
Oracle:子查询返回多行
数据库·oracle
计算机安禾26 分钟前
【数据库系统原理】第10篇:SQL高级查询机制:嵌套子查询与相关子查询的执行窥探
大数据·数据库·sql