前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >MySQL—内连接和外连接区别

MySQL—内连接和外连接区别

作者头像
全栈程序员站长
发布2022-09-27 10:35:47
1.2K0
发布2022-09-27 10:35:47
举报
文章被收录于专栏:全栈程序员必看

大家好,又见面了,我是你们的朋友全栈君。

区别

  • 内连接(inner join):取出两张表中匹配到的数据,匹配不到的不保留
  • 外连接(outer join):取出连接表中匹配到的数据,匹配不到的也会保留,其值为NULL

示例表

users表

代码语言:javascript
复制
mysql> select * from users;
+----+-------+
| id | name  |
+----+-------+
|  1 | john  |
|  2 | May   |
|  3 | Lucy  |
|  4 | Jack  |
|  5 | James |
+----+-------+
5 rows in set (0.00 sec)

topics表

代码语言:javascript
复制
mysql> select * from topics;
+----+---------------------------------------+---------+
| id | title                                 | user_id |
+----+---------------------------------------+---------+
|  1 |  Hello world                          |       1 |
|  2 | PHP is the best language in the world |       2 |
|  3 | Laravel artist                        |       6 |
+----+---------------------------------------+---------+
3 rows in set (0.00 sec)

内连接(inner join)

  • 示例
代码语言:javascript
复制
mysql> select * from users as u inner join topics as t on u.id=t.user_id;
+----+------+----+---------------------------------------+---------+
| id | name | id | title                                 | user_id |
+----+------+----+---------------------------------------+---------+
|  1 | john |  1 |  Hello world                          |       1 |
|  2 | May  |  2 | PHP is the best language in the world |       2 |
+----+------+----+---------------------------------------+---------+
2 rows in set (0.00 sec)

inner可以省略,as是给表起别名,也可以省略

代码语言:javascript
复制
mysql> select * from users u join topics t on u.id=t.user_id;
+----+------+----+---------------------------------------+---------+
| id | name | id | title                                 | user_id |
+----+------+----+---------------------------------------+---------+
|  1 | john |  1 |  Hello world                          |       1 |
|  2 | May  |  2 | PHP is the best language in the world |       2 |
+----+------+----+---------------------------------------+---------+
2 rows in set (0.00 sec)

以上两句等价于

代码语言:javascript
复制
mysql> select * from users,topics where users.id=topics.user_id;
+----+------+----+---------------------------------------+---------+
| id | name | id | title                                 | user_id |
+----+------+----+---------------------------------------+---------+
|  1 | john |  1 |  Hello world                          |       1 |
|  2 | May  |  2 | PHP is the best language in the world |       2 |
+----+------+----+---------------------------------------+---------+
2 rows in set (0.00 sec)

外连接(outer join)

  • 左外连接(left outer join):以左边的表为主表
  • 右外连接(right outer join):以右边的表为主表

以某一个表为主表,进行关联查询,不管能不能关联的上,主表的数据都会保留,关联不上的以NULL显示

通俗解释就是:先拿出主表的所有数据,然后到关联的那张表去找有没有符合关联条件的数据,如果有,正常显示,如果没有,显示为NULL

示例

代码语言:javascript
复制
mysql> select * from users as u left join topics as t on u.id=t.user_id;
+----+-------+------+---------------------------------------+---------+
| id | name  | id   | title                                 | user_id |
+----+-------+------+---------------------------------------+---------+
|  1 | john  |    1 |  Hello world                          |       1 |
|  2 | May   |    2 | PHP is the best language in the world |       2 |
|  3 | Lucy  | NULL | NULL                                  |    NULL |
|  4 | Jack  | NULL | NULL                                  |    NULL |
|  5 | James | NULL | NULL                                  |    NULL |
+----+-------+------+---------------------------------------+---------+
5 rows in set (0.00 sec)

等价于以下,只是字段的位置不一样

代码语言:javascript
复制
mysql> select * from topics as t right join users as u on u.id=t.user_id;
+------+---------------------------------------+---------+----+-------+
| id   | title                                 | user_id | id | name  |
+------+---------------------------------------+---------+----+-------+
|    1 |  Hello world                          |       1 |  1 | john  |
|    2 | PHP is the best language in the world |       2 |  2 | May   |
| NULL | NULL                                  |    NULL |  3 | Lucy  |
| NULL | NULL                                  |    NULL |  4 | Jack  |
| NULL | NULL                                  |    NULL |  5 | James |
+------+---------------------------------------+---------+----+-------+
5 rows in set (0.00 sec)

左外连接和右外连接是相对的,主要就是以哪个表为主表去进行关联

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/178383.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 区别
  • 内连接(inner join)
  • 外连接(outer join)
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档