MySQL基础之多表查询

目录

1,多表关系

        (1)一对多(多对一)

         (2)多对多

         (3)一对一

2,多表查询概述

        (1)概述

        (2)笛卡尔积

        (3)多表查询分类

3,内连接

4,外连接        

5,自连接

6,联合查询-union, union all

7,子查询

        (1)标量子查询

        (2)列子查询

        (3)行子查询

        (4)表子查询


1,多表关系

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

        (1)一对多(多对一)

                案例:部门与员工的关系

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

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

         (2)多对多

                案例:学生与课程的关系

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

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

                下面进行实战演示

-- 创建学生表并插入数据
create table student(
    id int primary key auto_increment comment '主键id',
    name varchar(10) comment '姓名',
    no varchar(10) comment '学号'
) comment '学生表';
insert into student(name, no) values ('张三', '2023060301'), ('李四', '2023060302'), ('王五', '2023060303'), ('赵六', '2023060304');

-- 创建课程表并插入数据
create table course(
    id int primary key auto_increment comment '主键id',
    name varchar(10) comment '课程名称'
) comment '课程表';
insert into course values (null, 'Java'), (null, 'PHP'), (null, 'MySQL'), (null, 'Hadoop');
select * from course;

-- 创建学生课程中间表并插入数据
create table student_course(
    id int primary key auto_increment comment '主键',
    studentid int not null comment '学生id',
    courseid int not null comment '课程id',
    constraint fk_courseid foreign key (courseid) references course (id),
    constraint fk_studentid foreign key (studentid) references student (id)
) comment '学生课程中间表';
insert into student_course (id, studentid, courseid)
values (null, 1, 1), (null, 1, 2), (null, 1, 3), (null, 2, 2), (null, 2, 3), (null, 3, 4);
select * from student_course;

         (3)一对一

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

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

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

2,多表查询概述

        (1)概述

                指从多张表中的查询数据。

        (2)笛卡尔积

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

-- 多表查询语法
select * from 表名1, 表名2...;
-- 执行该语句后,查询结果就会出现笛卡尔积现象,
-- 比如表1有3条数据,表2有2条数据,那么查询结果就会有3 * 2 = 6条语句,这显然不是我们想要的结果,
-- 尤其当两个表或多个表之间有联系时,就需要在查询的基础上添加条件,消除无效的笛卡尔积结果
select * from 表名1, 表名2... where 条件表达式;

        (3)多表查询分类

                ①连接查询

                        内连接:相当于查询A、B交际部分数据

                        外连接:

                                左外连接:查询左表所有数据,以及两张表交集部分数据

                                右外连接:查询右表所有数据,以及两张表交集部分数据

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

                ②子查询

3,内连接

        内连接查询的是两张表交集部分,即下图绿色部分。

-- 内连接查询语法:

-- 隐式内连接
-- select 字段列表 from 表1, 表2 where 条件...;

-- 显示内连接
-- select 字段列表 from 表1 [inner] join 表2 on 连接条件...;


-- 内连接演示
-- 1,查询每一个员工的姓名,及关联的部门的名称(隐式内连接实现)
-- 表结构:emp, dept
-- 连接条件:emp.dept_id = dept.id 
select emp.name, dept.name from emp, dept where emp.dept_id = dept.id;

-- 当表的名字很长时,可以给表起别名,例如上面这个语句就可以用如下写法
-- 当给表起别名之后,我们就不可以使用 表名.字段 了
select e.name, d.name from emp e, dept d where e.dept_id = d.id;


-- 显式内连接
-- 2,查询每一个员工的姓名,及关联的部门的名称(显式内连接实现)
-- ... inner join ... on ...
-- 表结构:emp, dept
-- 连接条件:emp.dept_id = dept.id 
select e.name, d.name from emp e inner join dept d on e.dept_id = d.id;

4,外连接        

-- 外连接查询语法

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

-- 右外连接
-- select 字段列表 from 表1 right [outer] join 表2 on 条件...;
-- 相当于查询表2(右表)的所有数据 包含 表1和表2交集部分的数据

 

-- 外连接演示
-- 1,查询emp表的所有数据,和对应的部门信息(左外连接)
-- 表结构:emp, dept
-- 连接条件:emp.dept_id = dept.id 
select e.*, d.name from emp e left outer join dept d on e.dept_id = d.id;

-- 2,查询dept表的所有数据,和对应的员工信息(右外连接)
select d.*, e.* from emp e right outer join dept d on e.dept_id = d.id;

5,自连接

-- 自连接查询语法
-- select 字段列表 from 表A 别名A join 表A 别名B on 条件 ...;
-- 自连接查询,可以是内连接查询,也可以是外连接查询

-- 自连接

-- 1,查询员工 及其  所属领导的名字
-- 表结构:emp
select a.name, b.name from emp.a, emp.b where a.managerid = b.id;

-- 2,查询所有员工 emp 及其领导的名字 emp,如果员工没有领导,也需要查询出来
-- 表结构:emp a, emp b
select a.name '员工', b.name '领导' from emp.a left join emp.b on a.managerid = b.id;

6,联合查询-union, union all

        对于union查询,就是把多次查询的结果合并起来,形成一个新的查询结果集。

-- select 字段列表 from 表A ...
-- union[all]
-- select 字段列表 from 表B ...; 

-- 1,将薪资低于5000的员工,和年龄大于50岁的员工全部查询出来

-- 使用union all,会将两次查询的结果直接合并在一起,如果有重复的记录,不会去重
select * from emp where salary < 5000
union all
select * from emp where age > 50;

-- 而union会将两次查询的结果的重复部分进行去重,只保留一条
select * from emp where salary < 5000
union all
select * from emp where age > 50;

        对于联合查询的多张表的列数必须一致,字段类型也需要保持一致。

        union all会将全部的数据直接合并在一起,union会对合并之后的数据去重。

7,子查询

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

-- 根据子查询结果不同,分为:
--   标量子查询(子查询结果为单个值)
--   列子查询(子查询结果为一列)
--   行子查询(子查询结果为一行)
--   表子查询(子查询结果为多行多列)

-- 根据子查询位置,分为:where之后,from之后,select之后。

        (1)标量子查询

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

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

-- 标量子查询

-- 1,查询"销售部"的所有员工信息

-- 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 = '销售部');


-- 2,查询在"方东白"入职之后的员工信息

-- a.查询 方东白的入职日期
-- select entrydate from emp where name = '方东白';
-- b.查询指定入职日期之后入职的员工信息
-- select * from emp where entrydate > '方东白的入职日期';

-- 合在一起
select * from emp where entrydate > (select entrydate from emp where name = '方东白');

        (2)列子查询

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

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

操作符描述
in在指定的集合范围之内,多选一
not in不在指定的集合范围之内

any

子查询返回列表中,有任意一个满足即可
some与any等同,使用some的地方都可以使用any
all子查询返回列表的所有值都必须满足
-- 列子查询

-- 1,查询"销售部"和"市场部"的所有员工信息
-- a.查询销售部和市场部的部门id
-- select id from dept where name = '销售部' or name = '市场部';
-- b.根据部门id,查询员工信息
select * from emp where dept_id in (select id from dept where name = '销售部' or name = '市场部');

-- 2,查询比财务部所有人工资都高的员工信息
-- a.查询所有 财务部 人员工资
-- select id from dept where name = '财务部';
-- select salary from emp where dept_id = (select id from dept where name = '财务部');
-- b.比 财务部 所有人工资都高的员工信息 
select * from emp where salary > all (select salary from emp where dept_id = (select id from dept where name = '财务部'));

-- 3,查询比研发部其中任意一人工资高的员工信息
-- a.查询研发部所有人工资
-- select salary from emp where dept_id = (select id from dept where name = '研发部');
-- b.比研发部其中任意一人工资高的员工信息
select * from emp where salary > any (select salary from emp where dept_id = (select id from dept where name = '研发部'));

        (3)行子查询

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

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

-- 行子查询
 
-- 1,查询与"张无忌"的薪资及直属领导相同的员工信息
-- a.查询"张无忌"的薪资及直属领导
-- select salary, managerid from emp where name = '张无忌';
-- b.查询与"张无忌"的薪资及直属领导相同的员工信息
select * from emp where (salary, managerid) = (select salary, managerid from emp where name = '张无忌');

        (4)表子查询

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

                常用的操作符:in

-- 表子查询

-- 1,查询与"鹿仗客","宋远桥"的职位和薪资相同的员工信息
-- a.查询与"鹿仗客","宋远桥"的职位和薪资
-- select job, salary from emp where name = '鹿仗客' or name = '宋远桥';
-- b.查询与"鹿仗客","宋远桥"的职位和薪资相同的员工信息
select * from emp where (job, salary) in (select job, salary from emp where name = '鹿仗客' or name = '宋远桥');

-- 2,查询入职日期是"2006-01-01"之后的员工信息,及其部门信息
-- a.查询入职日期是"2006-01-01"之后的员工信息
-- select * from emp where entrydate > '2006-01-01';
-- b.查询这部分员工,对应的部门信息
select e.*, d.* from (select * from emp where entrydate > '2006-01-01') e left join dept d on e.dept_id = d.id;

                 ​​​​​​​后续会举一些多表查询的例子实战练练手!!!

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值