MySQL—内连接和外连接区别

MySQL—内连接和外连接区别区别内连接(innerjoin):取出两张表中匹配到的数据,匹配不到的不保留 外连接(outerjoin):取出连接表中匹配到的数据,匹配不到的也会保留,其值为NULL示例表users表mysql>select*fromusers;+—-+——-+|id|name|+—-+——-+|1|john||2…


Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

区别

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

示例表

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)

  • 左外连接(left outer join):以左边的表为主表
  • 右外连接(right 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)

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

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

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

赞 (0)
全栈程序员-站长的头像全栈程序员-站长

相关推荐

  • 什么叫应用程序域?(zhuan)

    什么叫应用程序域?(zhuan) 一:应用程序域介绍:    "域",就是范围,环境,边界的意思,那么什么是应用程序域,官方给出的是这样的解释:操作系

    2022年7月3日
  • java.lang.Integer常用方法

    java.lang.Integer常用方法+构造函数Integer(intvalue)通过指定的int值构成一个Integer对象。Integer(Strings)通过指定的String值构成一个Integer对象。。+方法int intValue()将此对象转化为int。long longValue()将此对象转化为long。byte byteValue()将此对象转化为byte。sho…

    2022年7月16日
  • TP5 分页样式[通俗易懂]

    TP5 分页样式[通俗易懂]自定义分页类放到extend\page\,这里也可以自己决定,命名空间对了就行    在extend\page\下新建Page.php把以下代码粘过去<?phpnamespacepage;//+———————————————————————-//|ThinkPHP[WECAN…

    2022年7月17日
  • vs2008连接tfs2013_VS连接SQL网页登录

    vs2008连接tfs2013_VS连接SQL网页登录偶尔还是会用到,老是忘记安装的顺序,在这儿mark一下。用VS2008连接TFS2010,需要按照以下顺序安装一下组件:.VS2008TeamExplorer2008      3.InstallVisualStudio2008SP1      4.InstallVSTS2008ForwardCompatibilityUpdate

    2022年9月23日
  • android全面屏像素密度,手机屏幕分辨率、PPI像素密度科普知识大全「建议收藏」

    android全面屏像素密度,手机屏幕分辨率、PPI像素密度科普知识大全「建议收藏」手机屏幕分辨率、PPI像素密度科普知识大全【PPi指数】是手机清晰度的重要决定因素,所谓的PPI即每英寸所拥有的像素数目。现在市售的大屏幕手机普遍分辨率都只停留在854*480的水平,同样的分辨率,屏幕越大,像素点之间的距离越大,屏幕就越粗糙。所以大屏幕也不一定能带来良好的视觉感受。实践证明,ppi低于240的让人的视觉可以察觉明显颗粒感。ppi高于300则无法察觉理论上讲超过300ppi才没有…

    2022年6月11日
  • JsonPath使用

    JsonPath使用JSONPath-是xpath在json的应用。xml最大的优点就有大量的工具可以分析,转换,和选择性的提取文档中的数据。XPath是这些最强大的工具之一。如果可以使用xpath来解析json,以下的问题可以被解决: 1,数据不使用特殊的脚本,可以在客户端交互的发现并取并获取。2,客户机请求的JSON数据可以减少到服务器上的相关部分,这样可以最大限度地减少服务器响应的带宽使用…

    2022年6月18日

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号