五、多表查询-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;
相关推荐
geyasi几秒前
【Flask】四、flask连接并操作数据库
数据库·python·flask
金仓数据库1 分钟前
浙人医的国产软硬一体诊疗 | 电科金仓携手迈瑞打通PACS链路
大数据·数据库·人工智能
荒川之神16 分钟前
Oracle 开窗函数 专项练习文档
数据库·oracle
Java面试题总结26 分钟前
WAF 误杀了正常请求怎么补数据?CloudFront + Lambda@Edge 双函数架构实战
数据库·架构·edge
weixin_7042660528 分钟前
Redis集群架构与搭建全攻略
数据库·redis·架构
二进制_博客35 分钟前
使用Datax批量将mysql数据导入hive
数据库·hive·mysql
Hvitur1 小时前
软考架构师【第八章】系统质量属性与架构评估
数据库·架构
遇见你...1 小时前
B01 SpringMVC入门
数据库·sql
2601_949816161 小时前
使用python进行PostgreSQL 数据库连接
数据库·python·postgresql
脑子加油站1 小时前
mysql数据库常用命令
数据库