当前位置:网站首页>【MySQL】数据库多表链接的查询方式
【MySQL】数据库多表链接的查询方式
2022-08-05 05:13:00 【来一杯奶喵】
数据库的多表联查可以通过连接运算实现,即将多张表通过主外键关系关联在一起进行查询。
多表联查的方式主要分为两大类:非等值联查和等值联查。
首先先建立了两张表:student表、sc表。基于这两张表演示多表联查。
create table Student(
Sid int primary key auto_increment,
Sname varchar(10),
birthday datetime,
Ssex varchar(10),
classid int
);
create table SC(
Sid int,
Cid int,
score decimal(18,1),
foreign key(Sid) references Student(Sid),
foreign key(Cid) references Course(Cid)
);
非等值联查:
select * from student,sc
这种查询会产生笛卡尔积:也就是student表内所有数据以及sc表内所有数据的乘积。这种情况是由于没有有效的连接条件产生的,并不适用于表内存在大量数据的情况。为避免笛卡尔积的产生就需要用到等值联查。
等值联查:
select * from studend,sc where student.sid = sc.sid这种使用where子句的等值查询方式只适用于表内数据量小的情况,且只能挑选一次
等值查询有内联查询和外联查询。
我们先来看看内联查询(数据量大时使用,虚拟表按照数据量最大的表创建):
语法:select * from 表1 inner join 表2 on 条件
select * from student inner join sc on student.sid = sc.sid这条内联查询语句得到的数据是筛选两个表内相同sid的记录,至少有一行记录匹配,就能返回结果,如下图所示:

外联查询:左外联、右外联
左外联查询:显示主表全部信息,join左边为主查表
语法:select * from 表1 left join 表2 on 条件
select * from student left join sc on student.sid = sc.sid以student表为主表查询记录,所以没有成绩的学生记录也在查询结果中,如下图所示:

右外联查询: 显示主表全部信息,join右边为主查表
语法:select * from 表1 right join 表2 on 条件
select * from student right join sc on student.sid = sc.sid以sc表为主表查询记录,从sc中返回所有的记录,即便在student中没有匹配的行。
如下图所示:
总结:
多表联查的方式:
1.非等值联查,产生笛卡尔积,不适合数据量大时使用
2.等值查询,【单独where子句查询,内联查询,外联查询(左外联,右外联)】
①单纯使用where子句,同样不适合大量数据时使用
②内联查询 inner join
③左外联查询 left join (join左边为主表)
④右外联查询 right join (join右边为主表)
以上就是多表联查的几种方式,希望对看到这里的你有所帮助。与君共勉。
边栏推荐
- OFDM Lecture 16 5 -Discrete Convolution, ISI and ICI on DMT/OFDM Systems
- Opencv中,imag=cv2.cvtColor(imag,cv2.COLOR_BGR2GRAY) 报错:error:!_src.empty() in function ‘cv::cvtColor‘
- redis 持久化
- Flutter real machine running and simulator running
- [Go through 3] Convolution & Image Noise & Edge & Texture
- LeetCode: 1403. Minimum subsequence in non-increasing order [greedy]
- 【Over 16】Looking back at July
- Machine Learning (2) - Machine Learning Fundamentals
- "Recursion" recursion concept and typical examples
- Returned object not currently part of this pool
猜你喜欢

Pandas(五)—— 分类数据、读取数据库

RL reinforcement learning summary (1)

vscode+pytorch使用经验记录(个人记录+不定时更新)

【过一下3】卷积&图像噪音&边缘&纹理
![[Go through 7] Notes from the first section of the fully connected neural network video](/img/e2/1107171b52fe9dcbf454f7edcdff77.png)
[Go through 7] Notes from the first section of the fully connected neural network video

Flink 状态与容错 ( state 和 Fault Tolerance)

Basic properties of binary tree + oj problem analysis

vscode+pytorch use experience record (personal record + irregular update)

CAP+BASE

多线程查询结果,添加List集合
随机推荐
2022 Hangzhou Electric Multi-School 1st Session 01
【过一下10】sklearn使用记录
学习总结week3_3迭代器_模块
Basic properties of binary tree + oj problem analysis
学习总结week2_5
RDD和DataFrame和Dataset
vscode+pytorch use experience record (personal record + irregular update)
Distributed and Clustered
"Recursion" recursion concept and typical examples
The underlying mechanism of the class
物理层的接口有哪几个方面的特性?各包含些什么内容?
range函数作用
What are the characteristics of the interface of the physical layer?What does each contain?
【过一下9】卷积
Mysql5.7 二进制 部署
RL reinforcement learning summary (1)
Develop a highly fault-tolerant distributed system
将照片形式的纸质公章转化为电子公章(不需要下载ps)
day6-列表作业
Matplotlib(一)—— 基础