当前位置:网站首页>零基础自学SQL课程 | OUTER JOIN外连接
零基础自学SQL课程 | OUTER JOIN外连接
2022-06-11 07:31:00 【喵宁一】
大家好,我是宁一。
今天讲解SQL教程第12课:OUTER JOIN外连接。
外连接是左外连接(LEFT OUTER JOIN)、右外连接(RIGHT OUTER JOIN)、全外连接(FULL OUTER JOIN)的统称。
一般我们会省略掉OUTER,后面的课程会统称:
左外连接为左连接(LEFT JOIN)
右外连接为右连接(RIGHT JOIN)
全外连接为全连接(FULL JOIN)。
上节课讲的INNER JOIN内连接,结果相当于两表的交集,这节课我们讲的左连接和右连接,结果中除了交集,还包含了左/右表中的全部记录。

1、LEFT JOIN左连接
左连接就是将JOIN前面的表中所有记录都展示出来。
上节课举的实例,我们再来看看。
Teachers教师表链接Students学生表,通过教师字段Tid连接。

SELECT *
FROM Teachers AS t
JOIN Students AS s
ON t.Tid = s.Tid

结果中只显示了张三和李四两个老师,如果我们想要显示所有老师,不管这个老师有没有对应的学生,这个时候就应该用到LEFT JOIN 左连接了。就会将JOIN前面的Teachers表中所有记录都展示出来。
实例:Teachers教师表连接Students学生表,通过教师字段Tid连接,老师要全部显示出来。
实例解析:只需要将上方实例中的JOIN换成LEFT JOIN就可以了
SELECT *
FROM Teachers AS t
LEFT JOIN Students AS s
ON t.Tid = s.Tid

2、RIGHT JOIN右连接
右连接就是将JOIN后面的表中所有记录都展示出来。跟左连接是对应互通的。
比如我们可以将上面Teachers表和Students表互换,再将LEFT JOIN改成RIGHT JOIN,输出结果基本是一样的。
SELECT *
FROM Students AS s
RIGHT JOIN Teachers AS t
ON t.Tid = s.Tid

与LEFT JOIN输出的记录是一样的,唯一的区别是:
Students表中的列(Sid、Sname等)在前面,Teachers表中的列(Tid、Tname)在后面。
3、全连接 FULL JOIN
MySQL中不支持 FULL JOIN,实际业务中,基本不用全连接。
全连接的结果集结合了 LEFT JOIN 和 RIGHT JOIN 的结果集,大家了解一下就好了,有条件的话可以在 SQL Server 中测试。
4、多表外连接
多表外连接与我们上节课讲的内连接类似,我们可以对多个表(3个及以上)进行外连接。
虽然可以调换顺序通用LEFT JOIN 和 RIGHT JOIN,但是要合并的表比较多时,同时用LEFT JOIN 和 RIGHT JOIN会比较混乱,不容易理解,最好只用 JOIN 和 LEFT JOIN,不用 RIGHT JOIN。
基本语法:
SELECT <字段名>
FROM <表a>
LEFT JOIN <表b>
ON a.<字段名> = b.<字段名>
LEFT JOIN <表C>
ON a.<字段名> = c.<字段名>
实例:Teachers教师表同时连接Students学生表和Courses课程表,通过教师字段Tid连接,老师要全部显示出来。

实例解析:要将老师全部显示出来,我们将Teachers教师表放在LEFT JOIN 左连接前面就可以了。
SELECT *
FROM Teachers AS t
LEFT JOIN Students AS s
ON t.Tid = s.Tid
LEFT JOIN Courses AS c
ON t.Tid = c.Tid

5、自外连接
上节课我们讲过自连接,是通过JOIN将表自己连接起来,自外连接通常指,通过LEFT JOIN将表自己连接起来。
就像大厂经常出的留存问题,就会用到自外连接。
我们作为课后作业给大家讲解一下。
作业:
现场写一道SQL,给定用户表Users,求出次日留存用户数。
次日留存用户数:某日活跃的用户在次日仍旧活跃用户数。

示例答案:
作业解析:这个题现在讲解有点超前了,用到的日期函数、聚合函数、GROUP BY语句我们都还没学过,大家可以先看看,不会做的话也没关系,学完我们后面的课程,再回来看看,解答起来就很轻松了。
DATEDIFF函数是计算两个日期间隔天数。
聚合函数COUNT是返回匹配指定条件的行数。
GROUP BY语句是对结果集进行分组,通常结合聚合函数使用。
SELECT
a.log_date AS '日期',
COUNT(DISTINCT b.usr_id) AS '次日留存用户数'
FROM Users a
LEFT JOIN Users b
ON a.usr_id = b.usr_id
AND DATEDIFF(b.log_date,a.log_date)=1
GROUP BY a.log_date;
下节课,我们来讲讲UNION 联合查询。
点击关注,更新课程第一时间通知哦~
边栏推荐
- 【CF#693 (Div. 3)】B. Fair Division
- [STL source code analysis] summary notes (10): hashtable exploration
- C language inherits memory management mechanism (unfinished)
- 【AtCoder2387】+/- Rectangle
- 二、用户登录和注册
- 学 SQL 必须了解的10个高级概念
- Raspberry pie builds a full-featured NAS server (07): manage your library & read as you please
- Configuration software -- control import
- C+tinycthread implementation thread
- [analysis of STL source code] summary note (4): behind the scenes hero allocator
猜你喜欢

CRMEB/V4.4标准版打通版商城源码小程序公众号H5+App商城源码

Raspberry pie builds a full-featured NAS server (07): manage your library & read as you please
![[STL source code analysis] summary notes (7): ingenious deque](/img/da/8ec42bfdbbf1b5bd1c2e396c2213e2.jpg)
[STL source code analysis] summary notes (7): ingenious deque
![P5431 [template] multiplicative inverse 2](/img/63/1cb95a55c9ce9b92d6d55381d0215b.jpg)
P5431 [template] multiplicative inverse 2

Niuke wrong question 3.1

QObject usage skills -- control function class

Several transaction modes of Seata

【Oracle 数据库】奶妈式教程day04 排序查询

Directrix of ellipse
![[STL source code analysis] summary notes (12): functors and adapters](/img/6d/a3a9cde2c8792579af7505c2226914.jpg)
[STL source code analysis] summary notes (12): functors and adapters
随机推荐
CRMEB/V4.4标准版打通版商城源码小程序公众号H5+App商城源码
[STL source code analysis] summary note (2): overview of containers
【CF#693 (Div. 3)】B. Fair Division
C language judging big end and small end [consortium or pointer] big end and small end conversion
Miscellany C language
Create a form whose client area is 800 pixels by 600 pixels
Tetris preliminary
Ffmpeg extraction package format extracts AAC and customizes adtc header to realize arbitrary frame decoding
MFC debugger OutputDebugString override
RTMP protocol
QT 基于QScrollArea的界面嵌套移动
Use definite integral to calculate triangle area
Ffmpe a small demo to understand 80% of common APIs
Pat class A by category
20200730 T3 small B farm [maximum perimeter empty rectangle (monotone stack + line segment tree)] & "ROI 2017 day 2" learning track
No response from win10 explorer when dragging files
【AtCoder2387】+/- Rectangle
Installation de SQL Server 2008 (avec mot de passe), création d'une base de données, test de projet de formulaire C
Cartland number application
【AtCoder1998】Stamp Rally(整体二分+并查集)