当前位置: 首页 > news >正文

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

文章目录

    • 五、多表查询
      • 5.1 多表关系
      • 5.2 多表查询概述
      • 5.3 内连接
      • 5.4 外连接
      • 5.5 自连接
      • 5.6 联合查询
      • 5.7子查询
      • 5.8 总结


五、多表查询

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 总结

在这里插入图片描述

http://www.lryc.cn/news/370813.html

相关文章:

  • LabVIEW进行图像拼接的实现方法与优化
  • 纷享销客安全体系:安全合规认证
  • 推荐这两款AI工具,真的很好用
  • 装饰器在实际开发中的作用
  • JVM学习-监控工具(三)
  • GPU显卡计算能力怎么算?
  • Spark参数配置不合理的情况
  • 【OpenGL学习】OpenGL不同版本渲染管线汇总
  • 等保测评练习
  • 第十五届蓝桥杯大赛 国赛 pb组F题【括号与字母】(15分) 栈的应用
  • MYSQL 三、mysql基础知识 4(存储过程与函数)
  • 鸿蒙开发文件管理:【@ohos.statfs (statfs)】
  • C++和C语言到底有什么区别?
  • 【Centos】深度解析:CentOS下安装pip的完整指南
  • 半导体PW和NPW的一些小知识
  • 后端启动项目端口冲突问题解决
  • 【优选算法】优先级队列 {优先级队列解决TopK问题,利用大小堆维护数据流的中位数}
  • 11 IP协议 - IP协议头部
  • 【java】【python】leetcode刷题记录--二叉树
  • EVA-CLIP实战
  • 限定法术施放目标
  • 【通信原理】数字频带传输系统
  • 数据价值管理-数据验收标准
  • vue3模板语法总结
  • Spring Cloud 之 GateWay
  • 可转债全部历史因子数据,提供api支持
  • Python | C++ | MATLAB | Julia | R 市场流动性数学预期评估量
  • Android 常用开源库 MMKV 源码分析与理解
  • 大模型高级 RAG 检索策略之流程与模块化
  • TCPListen客户端和TCPListen服务器