含义:将两个或多个表的行结合起来展示。
有以下四种常用类型:
JOIN
: 等同INNER JOIN
(内连接、等值连接)。只会把筛选条件等值的内容查询出来。
LEFT JOIN
:左连接。 会匹配左边数据表的所有行,即使右边表无对应的数据。
数据生成的默认规则是: 先展示交集,再展示左边表的剩余数据。
交集数据产生的规则是,右边的每行数据执行一次,左边的所有数据就会遍历一次。(类似固定变量法:右边某行固定一次,左边所有数据遍历一次)
RIGHT JOIN
:右连接。会匹配右边数据表的所有行,即使左边表无对应的数据。
数据生成的默认规则是: 先展示交集,再展示右边表的剩余数据。
交集数据产生的规则是是,左边的每行数据执行一次,右边的所有数据遍历一次。(类似固定变量法:左边某行固定一次,右边所有数据遍历一次)
FULL JOIN
:全连接。 会把左连接+右连接的数据筛选出来。
应用场景:
有时候我们需要通过关联关系,来从两个或多个表中获取数据,为了展示更完整的数据数据,这时候就需要用JOIN来查询数据。
因为数据库中创建的每个表都有其主要的功能,在保证其主要功能的情况下,可能只会在表中存储另一个表的id,这个id在另一个表中极有可能为主键,我们就可以通过这个id来进行关联查询等。
事例演示:language表+person表
language表数据如下:11条数据
![](https://img.haomeiwen.com/i2364940/0ca7f226d69279f8.png)
person表数据如下:11条数据
![](https://img.haomeiwen.com/i2364940/b22536ecf9ce7a4b.png)
JOIN 等同:INNER JOIN
例1:根据
用户id
进行筛选。
共筛选出10条数据
。
因为左表的invention_userId =100001
没有能和右表userId
对应的数据,同理,右表的userId = 20001
没有能和左表invention_userId
对应的数据。
SELECT
c.id,
c.language_name,
c.invention_time,
c.hot,
d.invention_name,
d.country,
d.userId
FROM
LANGUAGE c
INNER JOIN person d ON c.invention_userId = d.userId;
-
筛选后数据如下:
image.png
例2:根据
语言的发明时间
进行筛选。
共筛选出23条数据
。
根据筛选出的数据,可以得知匹配的规则是:先从右表的invention_time=1994
开始,然后从左表中遍历找到invention_time=1994
的数据,找到就筛选出一条并记录展示。
SELECT
c.id,
c.language_name,
c.invention_time,
c.hot
FROM
LANGUAGE c
INNER JOIN person d ON c. invention_time = d. invention_time;
-
筛选后数据如下:
image.png
LEFT JOIN
例2:根据用户id进行筛选。
可以测试无对应数据的情况,因为左表某行的invention_userId为100001,右表没有能对应的数据
共筛选出11条数据
。
匹配的规则是:文章最上面有介绍。
SELECT
c.id,
c.language_name,
c.invention_time,
c.hot,
c.invention_userId,
d.userId
FROM
LANGUAGE c
LEFT JOIN person d ON c.invention_userId = d.userId;
-
筛选后数据如下:
image.png
例2:根据
语言的发明时间
进行筛选。
共筛选出23条数据
。
匹配的规则是:文章最上面有介绍。
SELECT
c.id,
c.language_name,
c.invention_time,
c.hot
FROM
LANGUAGE c
LEFT JOIN person d ON c.invention_time = d.invention_time;
-
筛选后数据如下:
image.png
RIGHT JOIN
例1: 根据用户id进行筛选。
可以测试无对应数据的情况,因为右表某行的userId为20001,左表没有能对应的数据
。
共筛选出11条数据
。
匹配的规则是:文章最上面有介绍。
SELECT
c.id,
c.language_name,
c.invention_time,
c.hot,
c.invention_userId,
d.userId
FROM
LANGUAGE c
RIGHT JOIN person d ON c.invention_userId = d.userId;
-
筛选后数据如下:
image.png
例2:根据
语言的发明时间
进行筛选。
共筛选出23条数据
。
匹配的规则是:文章最上面有介绍。
SELECT
c.id,
c.language_name,
c.invention_time,
c.hot
FROM
LANGUAGE c
RIGHT JOIN person d ON c.invention_time = d.invention_time;
-
筛选后数据如下:
image.png
FULL JOIN 由于MySQL不支持FULL JOIN,可改为UNION ALL进行测试,是等价的。
例1: 根据用户id进行筛选。
共筛选出22条数据
。其实就是11
条左连接的数据+11
条右连接的数据。
SELECT
c.id,
c.language_name,
c.invention_time,
c.hot,
c.invention_userId,
d.userId
FROM
LANGUAGE c
LEFT JOIN person d ON c.invention_userId = d.userId UNION ALL
SELECT
c.id,
c.language_name,
c.invention_time,
c.hot,
c.invention_userId,
d.userId
FROM
LANGUAGE c
RIGHT JOIN person d ON c.invention_userId = d.userId;
-
筛选后数据如下:
image.png
例2:根据
语言的发明时间
进行筛选。
共筛选出46条数据
。其实就是23
条左连接的数据+23
条右连接的数据。
SELECT
c.id,
c.language_name,
c.invention_time,
c.hot
FROM
LANGUAGE c
LEFT JOIN person d ON c.invention_time = d.invention_time UNION ALL
SELECT
c.id,
c.language_name,
c.invention_time,
c.hot
FROM
LANGUAGE c
RIGHT JOIN person d ON c.invention_time = d.invention_time
-
筛选后数据如下:
union all.jpg
网友评论