当前位置:网站首页>图解MySQL内连接、外连接、左连接、右连接、全连接......太多了
图解MySQL内连接、外连接、左连接、右连接、全连接......太多了
2022-08-01 10:13:00 【菜鸟教程*…*】
用两个表(a_table、b_table),关联字段a_table.a_id和b_table.b_id来演示一下MySQL的内连接、外连接( 左(外)连接、右(外)连接、全(外)连接)。
MySQL版本:Server version: 5.6.31 MySQL Community Server (GPL)
数据库表:a_table、b_table
主题:内连接、左连接(左外连接)、右连接(右外连接)、全连接(全外连接)
前提
建表语句
CREATE TABLE `a_table` (
`a_id` int(11) DEFAULT NULL,
`a_name` varchar(10) DEFAULT NULL,
`a_part` varchar(10) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8
CREATE TABLE `b_table` (
`b_id` int(11) DEFAULT NULL,
`b_name` varchar(10) DEFAULT NULL,
`b_part` varchar(10) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8
表测试数据
一、内连接
- 关键字:
inner join on
- 语句:
select * from a_table a inner join b_table b on a.a_id =b.b_id;
- 执行结果:
- 说明:组合两个表中的记录,返回关联字段相符的记录,也就是返回两个表的交集(阴影)部分。
二、左连接(左外连接)
- 关键字:
left join on/ left outer join on
- 语句:
select * from a_table a left join b_table b a.a_id=b.b_id;
- 执行结果:
- 说明:
left join 是left outer join的简写,它的全称是左外连接,是外连接中的一种。
左(外)连接,左表(a_table)的记录将会全部表示出来,而右表(b_table)只会显示符合搜索条件的记录。右表记录不足的地方均为NULL。
三、右连接(右外连接)
- 关键字:
right join on / right outer join on
- 语句:
select * from a_table a right outer join b_table b on a.a_id = b.b_id;
- 执行结果:
- 说明:
right join是right outer join的简写,它的全称是右外连接,是外连接中的一种。
与左(外)连接相反,右(外)连接,左表(a_table)只会显示符合搜索条件的记录,而右表(b_table)的记录将会全部表示出来。左表记录不足的地方均为NULL。
四、全连接(全外连接)
MySQL目前不支持此种方式,可以用其他方式替代解决。
五、补充,MySQL如何执行关联查询
MySQL认为任何一个查询都是一次“关联”,并不仅仅是一个查询需要到两个表匹配才叫关联,所以在MySQL中,每一个查询,每一个片段(包括子查询,甚至基于单表查询)都可以是一次关联。
当前MySQL关联执行的策略很简单:MySQL对任何关联都执行嵌套循环关联操作,即MySQL先在一个表中循环取出单条数据,然后在嵌套循环到下一个表中寻找匹配的行,依次下去,直到找到所有表中匹配的行为止。然后根据各个表匹配的行,返回查询中需要的各个列。请看下面的例子中的简单的查询:
查询语句:select tbl1.col1, tbl2.col2 from tbl1 inner join tbl2 using(col3) where tbl1.col1 in (5, 6);
假设MySQL按照查询中的表顺序进行关联操作,我们则可以用下面的伪代码表示MySQL将如何完成这个查询:
outer_iter = iterator over tbl1 where col1 in (5, 6)
outer_row = outer_iter.next
while outer_row
inner_iter = iterator over tbl2 where col3 = outer_row.col3
inner_row = inner_iter.next
while inner_row
output [ outer_row.col1, inner_row.col2]
inner_row = inner_iter.next
end
outer_row = outer_iter.next
end
上面的执行计划对于单表查询和多表关联查询都适用,如果是一个单表查询,那么只需要上面外层的基本操作。对于外连接,上面的执行过程仍然适用。例如,我们将上面的查询语句修改如下:
select tbl1.col1, tbl2.col2 from tbl1 left outer join tbl2 using(col3) where tbl1.col1 in (5, 6);
那么,对应的伪代码如下:
outer_iter = iterator over tbl1 where col1 in (5, 6)
outer_row = outer_iter.next
while outer_row
inner_iter = iterator over tbl2 where col3 = outer_row.col3
inner_row = inner_iter.next
if inner_row
while inner_row
output [ outer_row.col1, inner_row.col2]
inner_row = inner_iter.next
end
else
output [ outer_row.col1, null]
end
outer_row = outer_iter.next
end
说明:第五部分摘自《高性能MySQL 第三版》
边栏推荐
- 深度学习 | MATLAB实现GRU门控循环单元gruLayer参数设定
- Glassmorphism design style
- 【软件架构模式】MVVM模式和MVC模式区别
- C#/VB.NET 将PPT或PPTX转换为图像
- WPF 截图控件之绘制箭头(五)「仿微信」
- 解决new Thread().Start导致高并发CPU 100%的问题
- 基于ModelArts的物体检测YOLOv3实践【玩转华为云】
- 2022年7月31日--使用C#迈出第一步--使用C#中的数组和foreach语句来存储和循环访问数据序列
- 【cartographer ros】十: 延时和误差分析
- Small application project works WeChat gourmet recipes applet graduation design of finished product (1) the development profile
猜你喜欢
Comprehensive experiment BGP
How to implement deep copy in js?
Batch大小不一定是2的n次幂!ML资深学者最新结论
世界第4疯狂的科学家,在103岁生日那天去世了
已解决(pip安装库报错)Consider using the-- user option or check the permissions.
50.【Application of dynamic two-dimensional array】
redis
.NET性能优化-使用SourceGenerator-Logger记录日志
基于CAP组件实现补偿事务与消息幂等性
CTFshow,命令执行:web37
随机推荐
2022年7月31日--使用C#迈出第一步--使用 C# 创建具有约定、空格和注释的易读代码
报告:想学AI的学生数量已涨200%,老师都不够用了
redis
[Software Architecture Mode] The difference between MVVM mode and MVC mode
Browser shortcut keys
Small application project works WeChat gourmet recipes applet graduation design of finished product (1) the development profile
EasyRecovery热门免费数据检测修复软件
Introduction to ADAS
2022年7月31日--使用C#迈出第一步--使用C#中的数组和foreach语句来存储和循环访问数据序列
使用ESP32驱动QMA7981读取三轴加速度(带例程)
mysql login in cmd and basic operations of database and table
PDMan-国产免费通用数据库建模工具(极简,漂亮)
SAP ABAP OData 服务如何支持 $orderby (排序)操作试读版
Introduction to data warehouse layering (real-time data warehouse architecture)
ModelArts-based object detection YOLOv3 practice [play with HUAWEI CLOUD]
编码解码(btoa、encodeURIComponent、encodeURI、escape)
How to implement deep copy in js?
阿里腾讯面试一二
Enterprise WeChat group: robot timing reminder function database configuration
基于ModelArts的物体检测YOLOv3实践【玩转华为云】