当前位置:网站首页>MySQL基础——多表查询
MySQL基础——多表查询
2022-06-29 15:50:00 【白巧克力x】
目录
上篇文章我们学习了MySQL基础——约束,这篇文章我们学习MySQL基础——多表查询。
多表关系
在数据表中,各个表结构之间存在着各种关系(一对一、一对多、多对多)。
一对一关系:
示例:学生与学生详情的关系,一个学生对应一个详细情况,一个详细情况对应一个学生。
实现:在任意一方加入外键,关联另外一方的主键,并且设置外键为唯一的UNIQUE。
一对多关系:
示例:学生与班级的关系,一个班级对应多个学生,一个学生对应一个班级。
实现:在一对多关系中,在多的一方建立外键,指向一的一方的主键。
多对多关系:
示例:学生与课程的关系,一个学生可以选修多门课程,一门课程也可以给多个学生选择。
实现:在多对多关系中,建立第三张中间表,中间表至少包含两个外键,分别关联两方主键。
接下来我们通过一对多关系来演示多表查询,演示的数据表结构如下:

建表语句如下:
create table classtype(
id int auto_increment primary key comment '类型id',
name varchar(10) comment '类型名'
)comment '班级类型表';
create table students(
id int auto_increment primary key comment '学生id',
name varchar(10) comment '学生名',
age int comment '年龄',
gender char(1) comment '1:男,2:女',
phone char(11) comment '手机号',
friendid int comment '朋友id',
classid int comment '班级id',
constraint fk_students_classtype foreign key(classid) references classtype(id)
)comment '学生表';数据表数据如下:

添加数据语句如下:
# 添加班级类型数据
insert into classtype values (null,'Java'),(null,'Python'),(null,'MySQL'),(null,'Hadoop');
# 添加学生数据
insert into students values (null,'张三',18,'1','13700000000',2,1),(null,'李四',19,'1','13700000001',3,1),(null,'王五',18,'1','13700000000',3,1),(null,'诸葛亮',16,'1','13730000000',2,3),(null,'孙尚香',18,'2','13769999999',6,2),(null,'马克',16,'1','13755555555',1,2),(null,'武则天',16,'2','13666666666',5,4),(null,'艾琳',16,'2','13444444444',9,3),(null,'公孙离',20,'2','13733333333',7,4),(null,'李元芳',15,'1','1300000000',1,null);多表查询
多表查询可以分为连接查询、联合查询和子查询。
连接查询
连接查询有:
内连接:相当于查询A、B交集部分数据;
外连接:左(右)外连接,查询左(右)表所有数据,以及两张表交集部分数据;
自连接:当前表与自身的连接查询,自连接必须使用表别名。
内连接
内连接可以分为隐式内连接和显式内连接,语法格式如下:
# 隐式内连接
select 字段列表 FROM 表1,表2 where 条件.....;
# 显式内连接
select 字段列表 FROM 表1 [inner] join 表2 on 连接条件....;查询学生的所有信息,示例代码如下:
# 隐式内连接
select * from students,classtype where students.classid=classtype.id;
# 显式内连接
select * from students inner join classtype on students.classid=classtype.id;如下图所示:

由于id为10的学生classid为null,所以只显示了9条数据。
当我们的数据表名很长时,可以为数据表其别名,上面的代码可以改写为:
# 隐式内连接
select * from students s,classtype c where s.classid=c.id;
# 显式内连接
select * from students s inner join classtype c on s.classid=c.id;返回的结果是一样的。
外连接
外连接查询语法格式如下:
# 左外连接
select 字段列表 FROM 表1 left [outer] join 表2 on 条件...;
# 右外连接
select 字段列表 FROM 表1 right [outer] join 表2 on 条件...;查询学生的所有信息,示例代码如下:
# 左外连接
select * from students left outer join classtype on students.classid=classtype.id;
# 右外连接
select * from students right outer join classtype on students.classid=classtype.id;如下图所示:

自连接
自连接查询,可以是内连接查询,也可以是外连接查询,语法格式如下:
select 字段列表 from 表A 别名A join 表名A 别名B on 条件...;示例代码如下:
select a.name,b.name from students a join students b on a.id=b.friendid;如下图所示:

联合查询
联合查询就是把多次查询的结果合并起来,形成一个新的查询结果集,使用union、union all关键字连接,语法格式如下:
select 字段列表 from 表A union [all] select 字段列表 from 表B查询学生年龄大于17和学生性别为男的所有信息,示例代码如下:
select * from students where age>17;
select * from students where gender=1;
select * from students where age>17 union all select * from students where gender=1;如下图所示:

这样就把两个查询的结果合并在一起了。但没有去重,这时我们只需要把all去掉就可以去重了,代码如下:
select * from students where age>17 union select * from students where gender=1;如下图所示:

注意:在联合查询中,多张表的列数、字段类型必须保持一致。
子查询
在SQL语句中嵌套select语句,称为嵌套查询,又称子查询。语法格式如下:
select * from t1 where column1=(select column1 from t2);子查询外部的语句可以是insert、update、delete、select的任何一个。
根据子查询结果不同,分为:
标量子查询(子查询结果为单个值);
列子查询(子查询结果为一列);
行子查询(子查询结果为一行);
表子查询(子查询结果为多行多列)。
根据子查询位置,可以分为:where之后,from之后,select之后。
标量子查询
子查询返回的结果是单个值(数字、字符串、日期等),最简单的形式,这种子查询成为标量子查询。
常用的操作符:= 、<> 、 > 、 >= 、 < 、 <=。
查询班级为Python的所有学生信息,示例代码如下:
select * from students where classid=(select id from classtype where name='Python');如下图所示:

列子查询
子查询返回的结果是一列或多列,这种子查询称为列子查询。
常用的操作符:
IN:在指定的集合范围之内,多选一;
NOT IN :不在指定的集合范围之内;
ANY:子查询返回列表中,有任意一个满足即可;
SOME:子查询返回列表中,有任意一个满足即可;
ALL:子查询返回列表的所有值都必须满足。
查询学生班级为Python或Java的所有学生信息,示例代码如下:
select * from students where id in (select id from classtype where name='Python' or name='Java');如下图所示:

行子查询
子查询返回的结果是一行或多列,这种子查询称为行子查询。
常用的操作符:=、<>、IN 、NOT IN。
示例代码如下:
select * from students where id in (select id from classtype where friendid='2');
表子查询
子查询返回的结果是多行多列,这种子查询称为表子查询。
常用的操作符为IN。
查询与张三、孙尚香年龄和手机号相同的学生信息,示例代码如下:
select * from students where (age,phone) in (select age,phone from students where name='张三' or name='孙尚香');如下图所示:

好了,MySQL基础——多表查询就学到这里了,下篇文章学习MySQL基础——事务。
边栏推荐
- R语言plotly可视化:plotly可视化多个数据集归一化直方图(historgram)、设置不同的直方图使用不同的分箱大小(bin size)、在直方图的底部边缘添加边缘轴须图rug
- 天龙八部TLBB系列 - 如何让宠物学习十二满技能
- Science:大脑中睡眠的相互关联原因和结果
- 按键精灵打怪学习-窗口绑定保护技能和点击技能
- R语言plotly可视化:plotly可视化箱图、可视化多个分类变量的箱图(Several Box Plots)
- 真正的测试 =“半个产品+半个开发”?
- 蓝桥杯2015年CA省赛(填坑中)
- Tianlong Babu TLBB series - how to make pets learn twelve full skills
- Houdini图文笔记:VAT(3.0)导入UE4/5的设置向导[官方文档翻译]
- 如何在 WordPress 中创建登录页面
猜你喜欢

BS-GX-017基于SSM实现的在线考试管理系统

迪赛智慧数——其他图表(基本旭日图):毕业演讲高频词

Cortical traceability analysis of ERP

Time format GTM to Beijing time

路由汇总带来的三层环路-解决实验

卫龙辣条第三次冲刺上市:业绩增速下滑,刘卫平、刘福平提前套现

MySQL常用语句和命令汇总

发明了杀毒软件之后,他选择做一个极品混混

BOE: with the arrival of the peak season in the second half of the year, the promotion and the release of new products, the demand is expected to improve

Sophon kg upgrade 3.1: break down barriers between data and liberate enterprise productivity
随机推荐
如何在 WordPress 中嵌入 iFrame
Cortical traceability analysis of ERP
关于组织开展2022年南京市创新产品(第一批)申报工作的通知
golang操作NSQ分布式消息队列
The latest agenda of dtcc2022 China database technology conference was released
ROS2机器人f1tenth之CLI工具基础
STM32与GD32笔记
虚拟主机、WordPress 主机和云主机之间的区别
Privacy computing helps secure data circulation and sharing
实战 | 神奇的 conic-gradient 圆锥渐变
Magento 和 WordPress 的区别
Self taught programming can understand the code, but what if you can't write it yourself
Houdini图文笔记:VAT(3.0)导入UE4/5的设置向导[官方文档翻译]
面试官:说一下MySQL事务隔离级别?
Science: the interrelated causes and consequences of sleep in the brain
Volcano engine was selected into the first "panorama of edge computing industry" in China
小程序在产业互联网有「大」作为
时间格式化 GTM转北京时间
我想网上注册股票开户,如何操作?另外,手机开户安全么?
【crossbeam系列】5 crossbeam-util和crossbeam-queue:一些实用的小东西