• 【MySQL】多表查询


    多表查询

    表关系

    • 一对多(多对一)
    • 多对多
    • 一对一
    一对多

    案例:部门与员工
    关系:一个部门对应多个员工,一个员工对应一个部门
    实现:在多的一方建立外键,指向一的一方的主键

    多对多

    案例:学生与课程
    关系:一个学生可以选多门课程,一门课程也可以供多个学生选修
    实现:建立第三张中间表,中间表至少包含两个外键,分别关联两方主键

    例子:

    create table mid_table(
        id int auto_increment comment '主键' primary key ,
        department_id int not null comment '部门id',
        student_id int not null comment '学生id',
        constraint fk_department_id foreign key (department_id) references dept(id),
        constraint fk_student_id foreign key (student_id) references score(id)
    )   comment '中间表';
    
    insert into mid_table values (null,3,1),(null,2,3),(null,2,1),(null,3,2);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    可视化多表关系:

    右键mid_table->Diagrams->Show Visualization(Datagrip数据库管理工具)

    在这里插入图片描述

    一对一

    案例:用户与用户详情
    关系:一对一关系,多用于单表拆分,将一张表的基础字段放在一张表中,其他详情字段放在另一张表中,以提升操作效率
    实现:在任意一方加入外键,关联另外一方的主键,并且设置外键为唯一的(UNIQUE)

    多表查询

    合并查询(笛卡尔积,会展示所有组合结果):
    select * from employee, dept;

    笛卡尔积:两个集合A集合和B集合的所有组合情况(在多表查询时,需要消除无效的笛卡尔积)

    消除无效笛卡尔积:
    select * from score,dept where score.dept_id = dept.id;

    内连接查询

    内连接查询的是两张表交集的部分

    隐式内连接:
    SELECT 字段列表 FROM 表1, 表2 WHERE 条件 ...;

    显式内连接:
    SELECT 字段列表 FROM 表1 [ INNER ] JOIN 表2 ON 连接条件 ...;

    显式性能比隐式高

    例子:

    -- 内连接演示
    -- 1.查询每一个学生的姓名,及关联的部门的名称(隐式内连接实现)
    -- 表结构:score,dept
    -- 连接条件:score.dept_id = dept.id
    
    select s.name,d.name from score s,dept d where s.dept_id = d.id;
    
    
    -- 2.查询每一个学生的姓名,及关联的部门的名称(隐式内连接实现)
    select s.name,d.name from score s inner join dept d on s.dept_id = d.id;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    外连接查询

    左外连接:
    查询左表所有数据,以及两张表交集部分数据
    SELECT 字段列表 FROM 表1 LEFT [ OUTER ] JOIN 表2 ON 条件 ...;
    相当于查询表1的所有数据,包含表1和表2交集部分数据

    右外连接:
    查询右表所有数据,以及两张表交集部分数据
    SELECT 字段列表 FROM 表1 RIGHT [ OUTER ] JOIN 表2 ON 条件 ...;

    例子:

    -- 左外连接
    -- 查询score表的所有数据,和对应部门的信息
    select s.*,d.name from score s left outer join dept d on s.dept_id = d.id;
    
    -- 右外连接
    -- 查询dept表的所有数据,和对应学生的信息
    select d.*,s.* from score s right outer join dept d on s.dept_id = d.id;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    右连接可以查询到dept表的所有数据,但score表中学生的dept_id为null的数据则无法查询

    自连接查询

    当前表与自身的连接查询,自连接必须使用表别名

    语法:
    SELECT 字段列表 FROM 表A 别名A JOIN 表A 别名B ON 条件 ...;

    自连接查询,可以是内连接查询,也可以是外连接查询

    例子:

    -- 查询员工及其所属领导的名字
    select a.name, b.name from employee a, employee b where a.manager = b.id;
    -- 查询所有员工employee及其领导的名字,没有领导的也查询出来
    select a.name '员工', b.name '领导' from employee a left join employee b on a.manager = b.id;
    
    • 1
    • 2
    • 3
    • 4

    联合查询 union, union all

    把多次查询的结果合并,形成一个新的查询集

    语法:

    SELECT 字段列表 FROM 表A ...
    UNION [ALL]
    SELECT 字段列表 FROM 表B ...
    
    • 1
    • 2
    • 3
    注意事项
    • UNION ALL 会有重复结果,UNION 不会
    • 联合查询比使用or效率高,不会使索引失效

    子查询

    SQL语句中嵌套SELECT语句,称谓嵌套查询,又称子查询。
    SELECT * FROM t1 WHERE column1 = ( SELECT column1 FROM t2);
    子查询外部的语句可以是 INSERT / UPDATE / DELETE / SELECT 的任何一个

    根据子查询结果可以分为:

    • 标量子查询(子查询结果为单个值)
    • 列子查询(子查询结果为一列)
    • 行子查询(子查询结果为一行)
    • 表子查询(子查询结果为多行多列)

    根据子查询位置可分为:

    • WHERE 之后
    • FROM 之后
    • SELECT 之后
    标量子查询

    子查询返回的结果是单个值(数字、字符串、日期等)。
    常用操作符:- < > > >= < <=

    例子:

    -- 查询销售部所有员工
    select id from dept where name = '销售部';
    -- 根据销售部部门ID,查询员工信息
    select * from employee where dept_id = 4;
    -- 合并(子查询)
    select * from employee where dept_id = (select id from dept where name = '销售部');
    -- 查询xxx入职之后的员工信息
    select * from employee where entrydate > (select entrydate from employee where name = 'xxx');
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    列子查询

    返回的结果是一列(可以是多行)。

    常用操作符:

    操作符描述
    IN在指定的集合范围内,多选一
    NOT IN不在指定的集合范围内
    ANY子查询返回列表中,有任意一个满足即可
    SOME与ANY等同,使用SOME的地方都可以使用ANY
    ALL子查询返回列表的所有值都必须满足

    例子:

    -- 查询销售部和市场部的所有员工信息
    select * from employee where dept_id in (select id from dept where name = '销售部' or name = '市场部');
    -- 查询比财务部所有人工资都高的员工信息
    -- 先查询所有财务部的人员工资
    select salary from employee where dept_id = (select id from dept where name = '财务部')
    select * from employee where salary > all(select salary from employee where dept_dept = (select id from dept where name = '财务部'));
    -- 查询比研发部任意一人工资高的员工信息
    select * from employee where salary > any (select salary from employee where dept_id = (select id from dept where name = '研发部'));
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    行子查询

    返回的结果是一行(可以是多列)。
    常用操作符:=, <, >, IN, NOT IN

    例子:

    -- 查询与xxx的薪资及直属领导相同的员工信息
    select * from employee where (salary, manager) = (12500, 1);
    select * from employee where (salary, manager) = (select salary, manager from employee where name = 'xxx');
    
    • 1
    • 2
    • 3
    表子查询

    返回的结果是多行多列
    常用操作符:IN

    例子:

    -- 查询与xxx1,xxx2的职位和薪资相同的员工
    select * from employee where (job, salary) in (select job, salary from employee where name = 'xxx1' or name = 'xxx2');
    -- 查询入职日期是2006-01-01之后的员工,及其部门信息
    select e.*, d.* from (select * from employee where entrydate > '2006-01-01') as e left join dept as d on e.dept = d.id;
    
    • 1
    • 2
    • 3
    • 4

    总结案例:

    create table salgrade
    (
        grade int,
        losal int,
        hisal int
    ) comment '薪资等级表';
    
    insert into salgrade
    values (1, 0, 3000);
    insert into salgrade
    values (2, 3001, 5000);
    insert into salgrade
    values (3, 5001, 8000);
    insert into salgrade
    values (4, 8001, 10000);
    insert into salgrade
    values (5, 10001, 15000);
    insert into salgrade
    values (6, 15001, 20000);
    
    select e.name, e.job, d.name
    from employee e,
         dept d
    where e.dept_id = d.id;
    
    -- 查询年龄大于25岁的员工的姓名、年龄、职位、部门信息(显式内连接)
    select e.name, e.age, e.job, d.name
    from employee e
             inner join dept d on e.dept_id = d.id
    where age >= 25;
    
    -- 查询所有员工的工资
    
    select e.*, s.grade, s.losal, s.hisal
    from employee e,
         salgrade s
    where e.salary >= s.losal
      and e.salary <= s.hisal;
    
    select e.*, s.grade, s.losal, s.hisal
    from employee e,
         salgrade s
    where e.salary between s.losal and s.hisal;
    
    -- 查询"科技部"所有员工的信息及工资等级
    -- 表:employee,salgrade,dept
    -- 连接条件:e.salary between s.losal and s.hisal,e.dept_id=d.id
    -- 查询条件:d.name='科技部'
    
    select *
    from employee e,
         dept d,
         salgrade s
    where e.dept_id = d.id
      and (e.salary between s.losal and s.hisal)
      and d.name = '科技部';
    
    -- 查询低于本部门的平均工资的员工信息
    -- a.查询指定部门平均薪资     1
    select avg(e1.salary) from employee e1 where e1.dept_id=1;
    select avg(e1.salary) from employee e1 where e1.dept_id=2;
    
    -- b.查询低于本部门平均薪资的员工信息
    select * from employee e2 where salary < (select avg(e1.salary) from employee e1 where e1.dept_id=e2.dept_id);
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
  • 相关阅读:
    几种新能源汽车(纯电、插混、油混、增程)的区别
    机器学习强基计划4-2:通俗理解极大似然估计和极大后验估计+实例分析
    LabVIEW程序框图进行缩放
    mybatis的xml中<trim>标签的用法
    cesium开发入门(vue2)
    查询优化与并发控制[姊妹篇.第六弹]
    简述信息都有哪些特征?
    云原生强大且灵活的持续集成CI开源框架Tekton实战-上
    正点原子嵌入式linux驱动开发——Linux ADC驱动
    设计模式之代理模式的理解
  • 原文地址:https://blog.csdn.net/Jerryqjr/article/details/132796254