五、多表查询-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;
相关推荐
阿巴~阿巴~22 分钟前
Centos 7/8 安装 Redis
linux·服务器·数据库·redis·centos
刘大猫.24 分钟前
mysql数据库压缩
数据库·mysql·压缩·mysql数据库压缩·数据库压缩·数据库备份与压缩
oracle040628 分钟前
sql练习题单-知识点总结
数据库·sql
会飞的架狗师29 分钟前
【MySQL体系】第4篇:MySQL 查询优化实用技巧
数据库·mysql
lypzcgf1 小时前
Coze源码分析-资源库-编辑数据库-前端源码-核心组件
前端·数据库·源码分析·coze·coze源码分析·ai应用平台·agent平台
wei_shuo1 小时前
KingbaseES聚焦产品上线
数据库·kingbasees
AI浩1 小时前
Redis中的RPOP、BRPOP、LPOP 和 BLPOP
数据库·chrome·redis
数据和云2 小时前
从Databricks和Supabase看AI时代的中国数据库启示
数据库·人工智能
我科绝伦(Huanhuan Zhou)2 小时前
Oracle ADRCI工具全面使用指南:从基础到故障诊断实战
数据库·oracle
数据库生产实战2 小时前
Oracle LOB使用入门和简单使用,提供学习用的测试用例!
数据库·学习·oracle