mysql内连接和外连接有哪些区别

发布时间:2022-01-06 15:08:03 作者:iii
来源:亿速云 阅读:377

这篇文章主要介绍“mysql内连接和外连接有哪些区别”,在日常操作中,相信很多人在mysql内连接和外连接有哪些区别问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”mysql内连接和外连接有哪些区别”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

mysql内连接和外连接的区别:内连接会取出连接表中匹配到的数据,匹配不到的不保留;而外连接会取出连接表中匹配到的数据,匹配不到的也会保留,其值为NULL。

mysql内连接和外连接有哪些区别

本教程操作环境:windows7系统、mysql8版本、Dell G3电脑。

区别

示例表

users表

mysql> select * from users;
+----+-------+
| id | name  |
+----+-------+
|  1 | john  |
|  2 | May   |
|  3 | Lucy  |
|  4 | Jack  |
|  5 | James |
+----+-------+
5 rows in set (0.00 sec)

topics表

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)

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是给表起别名,也可以省略

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)

以上两句等价于

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)

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

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

示例

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)

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

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)

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

到此,关于“mysql内连接和外连接有哪些区别”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!

推荐阅读:
  1. mysql 连接查询(俗称连表查询)内连接、外连接、自然连接
  2. SQL内连接、外连接以及(+)号用法

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

mysql

上一篇:如何使用__delay_cycles达到延时让LED闪烁

下一篇:mysql如何实现除法保留两位小数

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》