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


相关推荐

  • Arduino编程之Serial.println()和Serial.print()

    Arduino编程之Serial.println()和Serial.print()Arduino编程之Serial.println()和Serial.print()Arduino的输出基本就用两个函数print和println,区别在于后者比前者多了回车换行Serial.println(data)从串行端口输出数据,跟随一个回车(ASCII13,或’r’)和一个换行符(ASCII10,或’n’)。这个函数所取得的值与Serial.print()一样。Ser…

    2022年10月3日
    7
  • springboot的启动流程图_@SpringBootApplication

    springboot的启动流程图_@SpringBootApplication首先会new一个SpringApplication然后在构造方法里初始化一些属性。判断应用类型是响应式REACTIVE的还是Web应用SERVLET去spring.factories文件加载初始化器ApplicationContextInitializer去spring.factories文件加载监听器ApplicationListener实例化之后执行run方法主体,run执行流程是基于观察者模式的,整个SpringBoot的启动流程就是各种事件的发布。获取并启用监听器Applicati..

    2025年9月5日
    3
  • BLSP接口_jcom接口

    BLSP接口_jcom接口http://huaqianlee.github.io/2016/04/27/Uav/Qualcomm-uav-blsp-port/概述BLSP是高通对于低速接口的一种管理方式,8074平台含有两个BLSP(BAMLow-SpeedPeripheral)块,对应于12个BLSP端口。每一个BLSP块含有最多六个QualcommUniversalPe

    2022年10月19日
    2
  • Android使用HttpClient方法和易错问题

    Android使用HttpClient方法和易错问题

    2021年9月1日
    175
  • Python3字符串替换replace(),translate(),re.sub()

    Python3字符串替换replace(),translate(),re.sub()Python3的字符串替换,这里总结了三个函数,和`translate()re.sub()`replace()python中的方法把字符串中的old(旧字符串)替换成new(新字符串

    2022年7月5日
    20
  • 2020年3月23日阿里笔试题[通俗易懂]

    2020年3月23日阿里笔试题[通俗易懂]2020年3月23日阿里笔试题题目描述题目分析  这是阿里的第二场笔试,本来觉得没啥好写的,一道排列组合,一道迷宫。没有什么发挥的空间。但是后来在和大家讨论的过程中,把这道题的公司给敲出来了,但是这公式也不能白敲,干脆写一篇文章总结一下。题目描述一共n个人,从中选出任意个人组成一队,我们不妨记为k,再从k个人选出一人做队长。题目分析  这是一个典型的排列组合问题,从n个人选出k个,可…

    2022年5月22日
    29

发表回复

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

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