【MySQL】_外连接

目录

[3.1 情况一:两个表数据一一对应](#3.1 情况一:两个表数据一一对应)

[3.2 情况二:两个表数据并非一一对应](#3.2 情况二:两个表数据并非一一对应)


本专栏关于联合查询已建立相应库与表,原文链接如下:

【MySQL】_联合查询基础表-CSDN博客

内连接原文如下:

【MySQL】_内连接-CSDN博客

基于以上内容,本篇介绍外连接;


内连接与外连接都是进行笛卡尔积计算,但是细节之处仍有差别:

3.1 情况一:两个表数据一一对应

基于以下数据库与表:

sql 复制代码
mysql> select* from student;
+------+------+
| id   | name |
+------+------+
|    1 | 张三 |
|    2 | 李四 |
|    3 | 王五 |
+------+------+
3 rows in set (0.00 sec)

mysql> select* from score;
+------------+-------+
| student_id | score |
+------------+-------+
|          1 |    90 |
|          2 |    80 |
|          3 |    70 |
+------------+-------+
3 rows in set (0.00 sec)

内连接指令为:

sql 复制代码
mysql> select name, score from student join score on student.id = score.student_id;

左外连接指令为:

sql 复制代码
mysql> select name, score from student left join score on student.id = score.student_id;

右外连接指令为:

sql 复制代码
mysql> select name, score from student left join score on student.id = score.student_id;

以上三条指令的查询结果均为:

sql 复制代码
+------+-------+
| name | score |
+------+-------+
| 张三 |    90 |
| 李四 |    80 |
| 王五 |    70 |
+------+-------+

即:当两个表的数据一一对应(即两个表的记录在彼此表中都有体现)时,内连接与外连接的查询结果是相同的

3.2 情况二:两个表数据并非一一对应

基于以下数据库和表:

sql 复制代码
mysql> select* from student;
+------+------+
| id   | name |
+------+------+
|    1 | 张三 |
|    2 | 李四 |
|    3 | 王五 |
+------+------+
3 rows in set (0.00 sec)

mysql> select* from score;
+------------+-------+
| student_id | score |
+------------+-------+
|          1 |    90 |
|          2 |    80 |
|          4 |    70 |
+------------+-------+
3 rows in set (0.00 sec)

内连接指令与查询结果为:

sql 复制代码
mysql> select name, score from student join score on student.id = score.student_id;
+------+-------+
| name | score |
+------+-------+
| 张三 |    90 |
| 李四 |    80 |
+------+-------+
2 rows in set (0.00 sec)

左外连接指令与查询结果为:

sql 复制代码
mysql> select name, score from student left join score on student.id = score.student_id;
+------+-------+
| name | score |
+------+-------+
| 张三 |    90 |
| 李四 |    80 |
| 王五 |  NULL |
+------+-------+
3 rows in set (0.00 sec)

右外连接指令与查询结果为:

sql 复制代码
mysql> select name, score from student right join score on student.id = score.student_id;
+------+-------+
| name | score |
+------+-------+
| 张三 |    90 |
| 李四 |    80 |
| NULL |    70 |
+------+-------+
3 rows in set (0.00 sec)

即:当两个表的数据并非一一对应时,内连接只显示两个表中都有体现的数据

注:(1)当两个表数据并非一一对应,进行外连接时,左外连接是以左表为准,右表没有对应的数据,则以空值填充

右外连接是以右表为准,左表没有对应数据,以空值填充;

(2)join on针对多个表进行的语句为:

select* form 表1 join 表2 on 条件1 join 表4 on 条件2,如:

sql 复制代码
mysql> select* from student join score on student.id = score.student_id
    -> join course on course.id =score.course_id;

但是每次join on 语句都只计算两个表的笛卡尔积;

相关推荐
羑悻的小杀马特几秒前
从零搭建群晖私有影音库:NasTool自动化追剧全流程拆解与远程访问协议优化实践
运维·数据库·自动化
游戏开发爱好者81 小时前
iOS 26 iPhone 使用记录分析 多工具组合构建全方位设备行为洞察体系
android·ios·小程序·uni-app·cocoa·iphone·webview
TDengine (老段)2 小时前
杨凌美畅用 TDengine 时序数据库,支撑 500 条产线 2 年历史数据追溯
大数据·数据库·物联网·时序数据库·tdengine·涛思数据
葛小白15 小时前
C#数据类型:string简单使用
服务器·数据库·c#
污斑兔5 小时前
MongoDB的$sample是啥?
数据库·mongodb
马丁的代码日记7 小时前
MySQL InnoDB 行锁与死锁排查实战演示
数据库·mysql
拍客圈8 小时前
数据主站+副站做的设置
数据库
计算机学长felix8 小时前
基于SpringBoot的“面向校园的助力跑腿系统”的设计与实现(源码+数据库+文档+PPT)
数据库·spring boot·后端
zhangphil8 小时前
HARDWARE 属性的Bitmap与普通Bitmap,GPU与RenderThread渲染与处理方式异同比较,Android
android
金仓拾光集9 小时前
__工艺数据管理的范式转变:金仓数据库替代MongoDB实操实践__
数据库·mongodb