Mysql学习(八)——多表查询


五、多表查询

5.1 多表关系

  • 概述:项目开发中,在进行数据库表结构设计时,会根据业务需求及业务模块之间的关系,分析并设计表结构,由于业务之间相互关联,所以各个表结构之间也存在着各种联系,基本上分为三种:

    • 一对多(多对一)

    案例:部门与员工的关系

    关系:一个部门对应多个员工,一个员工对应一个部门

    实现:在多的一方建立外键,指向一的一方主键

    在这里插入图片描述

    • 多对多

    案例:学生与课程的关系

    关系:一个学生可以选修多门课程,一门课程也可以供多个学生选择

    实现:建立第三张中间表,中间表至少包含两个外键,分别关联两方主键

    在这里插入图片描述

    -- 创建学生表
    create table student(
        id int auto_increment primary key ,
        name varchar(10),
        no varchar(10)
    )comment '学生表';
    insert into student values (null,'黛绮丝','2000100101'),
                               (null,'谢逊','2000100102'),
                               (null,'殷天正','2000100103'),
                               (null,'韦一笑','2000100104');
    -- 创建课程表
    create table course(
        id int auto_increment primary key,
        name varchar(10)
    )comment '课程表';
    insert into course values (null,'java'),
                              (null,'PHP'),
                              (null,'MySQL'),
                              (null,'Hadoop');
    -- 创建学生课程中间表
    create table student_course(
        id int auto_increment primary key ,
        studentid int not null ,
        courseid int not null ,
        constraint fk_courseid foreign key (courseid) references course(id),
        constraint fk_studentid foreign key (studentid) references student(id)
    )comment '学生课程中间表';
    insert into student_course values (null,1,1),(null,1,2),(null,1,3),(null,2,2),(null,2,3),(null,3,4);
    
    • 一对一

    案例:用户与用户详情的关系

    关系:一对一关系,多用于单表拆分,将一张表的基础字段放在一张表中,其他详情字段放在另一张表中,以提升操作效率

    实现:在任意一方加入外键,关联另一方的主键,并且设置外键为唯一的(unique)

    在这里插入图片描述

    在这里插入图片描述

5.2 多表查询概述

  • 概述:指从多张表中查询数据
  • 笛卡尔积:笛卡尔乘积是指在数学中,两个集合A集合和B集合的所有组合情况。(在多表查询时,需要消除无效的笛卡尔积)

在这里插入图片描述

消除无效的笛卡尔积之后:

在这里插入图片描述

select * from1,2 where1外键字段 =2关联的字段;
  • 多表查询分类:
    • 连接查询:
      • 内连接:相当于查询A,B交集部分数据
      • 外连接:
        • 左外连接:查询左表所有数据,以及两张表交集部分数据
        • 右外连接:查询右表所有数据,以及两张表交集部分数据
      • 自连接:当前表与自身的连接查询,自连接必须使用表别名
    • 子查询

5.3 内连接

  • 隐式内连接
select 字段列表 from1,2 where 条件…;

select emp.name,dept.name from emp , dept where dept_id = dept.id;
  • 显式内连接
select 字段列表 from1 [inner] join2 on 连接条件…;

select e.name,d.name from emp e inner join dept d on dept_id = d.id;

在这里插入图片描述

5.4 外连接

  • 左外连接
-- 相当于查询表1(左表)的所有数据包含表1和表2交集部分的数据
select 字段列表 from1 left [outer] join2 on 条件…;

select e.*, d.name from emp e left outer join  dept d on e.dept_id = d.id;
  • 右外连接
-- 相当于查询表2(右边)的所有数据包含表1和表2交集部分的数据
select 字段列表 from1 right [outer] join2 on 条件…;

select e.name,d.* from emp e right join dept d on e.dept_id = d.id;

5.5 自连接

  • 自连接查询语法:
select 字段列表 from 表A 别名A join 表A 别名B on 条件…;
-- 自连接查询可以是内连接查询也可以是外连接查询。
select a.name ,b.name from emp a , emp b where a.managerid = b.id;
select a.name ,b.name from emp a left outer join emp b on a.managerid = b.id;

5.6 联合查询

  • 对于union查询,就是把多次查询的结果合并起来形成一个新的查询结果集。
select 字段列表 from 表A …
union [all]
select 字段列表 from 表B …;
-- 直接合并
select * from emp where salary < 5000
union all
select * from emp where age > 50;
-- 去重后的合并
select * from emp where salary < 5000
union
select * from emp where age > 50;

注意:对于联合查询的多张表的列数必须保持一致,字段类型也需要保持一致。union all会将全部的数据直接合并在一起,union会对合并之后的数据去重。

5.7子查询

  • 概念:SQL语句中嵌套select语句,称为嵌套语句,又称子查询。
select * from t1 where column1 = (select column1 from t2);
/*
子查询外部的语句可以是insert/update/delete/select的任何一个。
*/
  • 根据子查询结果不同,分为:

    • 标量子查询(子查询结果为单个值)

    子查询返回的结果是单个值(数字、字符串、日期等),最简单的形式,这种子查询称为标量子查询。

    常用的操作符:= <> > >= < <=

    -- 标量子查询
    -- 查询“销售部”的所有员工信息
    -- a 查询“销售部”部门ID
    select id from dept where name = '销售部';
    -- b 根据销售部门ID,查询员工信息
    select * from emp where dept_id = 4;
    -- 等价于
    select * from emp where dept_id = (select id from dept where name = '销售部');
    
    • 列子查询(子查询结果为一列)

    子查询返回的结果是一列(可以是多行),这种子查询称为列子查询。

    常用的操作符:in not in any some all

    在这里插入图片描述

    select * from emp where dept_id in (select id from dept where name = '销售部' or name = '市场部');
    
    -- 查询比财务部所有人工资都高的员工信息
    select * from emp where salary > all(select salary from emp where dept_id = (select id from dept where name = '财务部'));
    
    • 行子查询(子查询结果为一行)

    子查询返回的结果是一行(可以是多列),这种子查询称为行子查询。

    常用的操作符:= <> in not in

    select * from emp where (salary,managerid) = (select salary,managerid from emp where name = '张无忌');
    
    • 表子查询(子查询结果为多行多列)

    子查询返回的结果是多行多列,这种子查询称为表子查询。

    常用的操作符:in

    select * from emp where (job,salary) in (select job,salary from emp where name = '鹿杖客' or name = '宋远桥');
    
  • 根据子查询位置,分为:where之后、from之后和select之后。

5.8 总结

在这里插入图片描述

相关推荐

  1. MySQL - 查询

    2024-06-15 00:44:02       41 阅读
  2. MySQL-查询

    2024-06-15 00:44:02       41 阅读
  3. MySQL查询

    2024-06-15 00:44:02       29 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-06-15 00:44:02       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-06-15 00:44:02       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-06-15 00:44:02       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-06-15 00:44:02       18 阅读

热门阅读

  1. PHP异常处理的最佳实践及常见问题解决

    2024-06-15 00:44:02       12 阅读
  2. Set up an Ethereum node on your Raspberry Pi using IPv6

    2024-06-15 00:44:02       9 阅读
  3. HCIP认证笔记(判断题)

    2024-06-15 00:44:02       5 阅读
  4. 2024.6.12总结

    2024-06-15 00:44:02       11 阅读
  5. 编程前端看什么书比较好:深入解析与推荐

    2024-06-15 00:44:02       7 阅读
  6. 深入解析 Unix I/O 的五种模型

    2024-06-15 00:44:02       9 阅读
  7. mysql-线上常用运维sql-2

    2024-06-15 00:44:02       10 阅读