五、多表查询-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;
相关推荐
m0_5951998519 分钟前
Redis(以Django为例,含具体操作步骤)
数据库·redis·缓存
爱尚你199325 分钟前
MySQL 三大日志:redo log、undo log、binlog 详解
数据库·mysql
小猿姐2 小时前
KubeBlocks AI:AI时代的云原生数据库运维探索
数据库·人工智能·云原生·kubeblocks
NocoBase3 小时前
10 个开源工具,快速构建数据应用
数据库·低代码·开源
麻辣清汤4 小时前
结合BI多维度异常分析(日期-> 商家/渠道->日期(商家/渠道))
数据库·python·sql·finebi
Kan先生5 小时前
对象存储解决方案:MinIO 的架构与代码实战
数据库·python
超级迅猛龙5 小时前
保姆级Debezium抽取SQL Server同步kafka
数据库·hadoop·mysql·sqlserver·kafka·linq·cdc
杨过过儿6 小时前
【Task02】:四步构建简单rag(第一章3节)
android·java·数据库
····懂···6 小时前
攻克PostgreSQL专家认证
数据库·postgresql
每天都在想吃啥7 小时前
day31 SQLITE
数据库·sqlite