MySQL—内连接和外连接区别

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

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 程序员的搞怪代码诗建议收藏

    满园春色关不住一串代码飘出来。日照屏幕直冒烟,遥看代码挂前川。春眠不觉晓,起来敲代码。春宵一刻值千金,完事起来敲代码。洛阳亲友如相问,就说我在敲代码。夜阑卧听风吹雨,做梦还在敲代码。举头望明月,低

    2021年12月21日
    65
  • 【可视化爬虫】scrapinghub 可视化抓取 portia环境搭建全过程

    【可视化爬虫】scrapinghub 可视化抓取 portia环境搭建全过程文章目录scrapinghub可视化抓取portia环境搭建全过程一、install_deps:安装系统级依赖二、install_splash:安装splash三、install_python_deps:安装python依赖四、安装ember.js五、安装并配置nginx六、ember.js依赖安装scrapinghub可视化抓取portia环境搭建全过程一、insta…

    2025年6月3日
    3
  • KaOS Linux放出最新版ISO镜像喜迎五周岁

    KaOS Linux放出最新版ISO镜像喜迎五周岁

    2022年4月2日
    68
  • centOS7安装nginx及nginx配置「建议收藏」

    centOS7安装nginx及nginx配置「建议收藏」安装所需插件1、安装gccgcc是linux下的编译器在此不多做解释,感兴趣的小伙伴可以去查一下相关资料,它可以编译C,C++,Ada,ObjectC和Java等语言命令:查看gcc版本gcc-v一般阿里云的centOS7里面是都有的,没有安装的话会提示命令找不到,安装命令:yum-yinstallgcc2、pcre、pcre-…

    2022年4月27日
    120
  • 开源 串口调试助手 BaoYuanSerial 使用教程「建议收藏」

    开源 串口调试助手 BaoYuanSerial 使用教程「建议收藏」简介:软件使用.Net5+Avalonia实现跨平台方案。支持LinuxUbuntu,Windows,已在Ubuntu20.04,Win10Professional20H2测试通过。官方下载地址:项目地址:xuyuanbao/BaoYuanSerial:AGUISerialDebugToolforLinux/MicrosfotWindow(github.com)下载地址:ReleaseBaoYuanSerila-V1.1·xuyuanbao/BaoYuanSer

    2022年4月30日
    103
  • jrtplib java,jrtplib 分包处理

    转载自:http://blog.csdn.net/sxcong/article/details/3736354听说jrtplib写的不错,终于找到时间下来看看。下载,直接用VC6编译,很容易。然后打开VC,建立工程,测试examples下那几个收发程序,的确用起来很简单。想想以前都是自己封装UDP,现在的程序员真幸福。不过,在发送视频数据时出了问题,跟踪进去看了一下,里面设置最大帧数据长度为140…

    2022年4月17日
    46

发表回复

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

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