五、多表查询-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;
相关推荐
小白勇闯网安圈18 分钟前
Django Form 组件详解:从表单生成到 ModelForm 数据校验
数据库·django·sqlite
梦想不只是梦与想1 小时前
MySQL 数据库(二):数据类型
数据库·mysql·数据类型
野生技术架构师1 小时前
Redis 和 MySQL 如何保证数据一致性?先更新数据库还是先删缓存,延迟双删、MQ、Canal 一次讲透
数据库·redis·缓存
鸽芷咕1 小时前
SQL Server数据迁移到金仓数据库复盘:从怕性能翻车,到TPS提升60%
数据库
HAYDENR2 小时前
数据库如何做性能优化?数据库性能调优有哪些常见注意事项?
数据库·性能优化
Light Gao2 小时前
企业级灰度发布技术方案
网络·数据库·oracle
一水2 小时前
AI 时代审查思维:审查第一篇
java·jvm·数据库·spring
赵广陆2 小时前
企业实战:Milvues向量数据库实践
数据库·pycharm·langchain
微学AI2 小时前
把时间序列真正用起来:TimechoAI 使用与时序分析实战
数据库·人工智能·大模型
cspttty2 小时前
管理类专业证书含金量排名
数据库