转自:https://blog.csdn.net/qq_35975416/article/details/78842958

sql查询中有一个非常重要的环节就是表的关联查询,一般使用left join,right join,inner join,他们之间的区别是什么呢?

下面我们通过具体的sql语句来演示,演示用的表名为test1/test2:

mysql> select * from test1;
+----+--------+------+------+
| id | name   | age  | sex  |
+----+--------+------+------+
|  0 | 小董   | 20   | 男   |
|  1 | 小王   | 15   | 男   |
|  2 | 小李   | 18   | 男   |
|  3 | 小红   | 16   | 女   |
+----+--------+------+------+
4 rows in set (0.00 sec)

mysql> select * from test2;
+----+--------+-----------------------+-------+
| id | name   | interest              | score |
+----+--------+-----------------------+-------+
|  1 | 小王   | 打篮球                | 81    |
|  2 | 小李   | 踢足球                | 84    |
|  3 | 小红   | 看动漫                | 99    |
|  4 | 小军   | 打游戏,打篮球        | 100   |
+----+--------+-----------------------+-------+
4 rows in set (0.00 sec)

mysql> select * from test1 t1 left join test2 t2 on t1.id=t2.id and t1.name=t2.name;
+----+--------+------+------+------+--------+-----------+-------+
| id | name   | age  | sex  | id   | name   | interest  | score |
+----+--------+------+------+------+--------+-----------+-------+
|  1 | 小王   | 15   | 男   |    1 | 小王   | 打篮球    | 81    |
|  2 | 小李   | 18   | 男   |    2 | 小李   | 踢足球    | 84    |
|  3 | 小红   | 16   | 女   |    3 | 小红   | 看动漫    | 99    |
|  0 | 小董   | 20   | 男   | NULL | NULL   | NULL      | NULL  |
+----+--------+------+------+------+--------+-----------+-------+
4 rows in set (0.12 sec)

mysql> select * from test2 t1 left join test1 t2 on t1.id=t2.id and t1.name=t2.name;
+----+--------+-----------------------+-------+------+--------+------+------+
| id | name   | interest              | score | id   | name   | age  | sex  |
+----+--------+-----------------------+-------+------+--------+------+------+
|  1 | 小王   | 打篮球                | 81    |    1 | 小王   | 15   | 男   |
|  2 | 小李   | 踢足球                | 84    |    2 | 小李   | 18   | 男   |
|  3 | 小红   | 看动漫                | 99    |    3 | 小红   | 16   | 女   |
|  4 | 小军   | 打游戏,打篮球        | 100   | NULL | NULL   | NULL | NULL |
+----+--------+-----------------------+-------+------+--------+------+------+
4 rows in set (0.00 sec)

mysql> select t1.id,t1.name,t1.interest,t1.score,t2.age,t2.sex from test2 t1 left join test1 t2 on t1.id=t2.id and t1.name=t2.name;
+----+--------+-----------------------+-------+------+------+
| id | name   | interest              | score | age  | sex  |
+----+--------+-----------------------+-------+------+------+
|  1 | 小王   | 打篮球                | 81    | 15   | 男   |
|  2 | 小李   | 踢足球                | 84    | 18   | 男   |
|  3 | 小红   | 看动漫                | 99    | 16   | 女   |
|  4 | 小军   | 打游戏,打篮球        | 100   | NULL | NULL |
+----+--------+-----------------------+-------+------+------+
4 rows in set (0.02 sec)

mysql> select t1.id,t1.name,t1.interest,t1.score,t2.age,t2.sex from test2 t1 inner join test1 t2 on t1.id=t2.id and t1.name=t2.name;
+----+--------+-----------+-------+------+------+
| id | name   | interest  | score | age  | sex  |
+----+--------+-----------+-------+------+------+
|  1 | 小王   | 打篮球    | 81    | 15   | 男   |
|  2 | 小李   | 踢足球    | 84    | 18   | 男   |
|  3 | 小红   | 看动漫    | 99    | 16   | 女   |
+----+--------+-----------+-------+------+------+
3 rows in set (0.00 sec)

mysql> select * from test1 t1 left join test2 t2 on t1.id=t2.id and t1.name=t2.name;
+----+--------+------+------+------+--------+-----------+-------+
| id | name   | age  | sex  | id   | name   | interest  | score |
+----+--------+------+------+------+--------+-----------+-------+
|  1 | 小王   | 15   | 男   |    1 | 小王   | 打篮球    | 81    |
|  2 | 小李   | 18   | 男   |    2 | 小李   | 踢足球    | 84    |
|  3 | 小红   | 16   | 女   |    3 | 小红   | 看动漫    | 99    |
|  0 | 小董   | 20   | 男   | NULL | NULL   | NULL      | NULL  |
+----+--------+------+------+------+--------+-----------+-------+
4 rows in set (0.00 sec)

mysql> select * from test1 t1 right join test2 t2 on t1.id=t2.id and t1.name=t2.name;
+------+--------+------+------+----+--------+-----------------------+-------+
| id   | name   | age  | sex  | id | name   | interest              | score |
+------+--------+------+------+----+--------+-----------------------+-------+
|    1 | 小王   | 15   | 男   |  1 | 小王   | 打篮球                | 81    |
|    2 | 小李   | 18   | 男   |  2 | 小李   | 踢足球                | 84    |
|    3 | 小红   | 16   | 女   |  3 | 小红   | 看动漫                | 99    |
| NULL | NULL   | NULL | NULL |  4 | 小军   | 打游戏,打篮球        | 100   |
+------+--------+------+------+----+--------+-----------------------+-------+
4 rows in set (0.00 sec)

mysql> select * from test1 t1 inner join test2 t2 on t1.id=t2.id and t1.name=t2.name;
+----+--------+------+------+----+--------+-----------+-------+
| id | name   | age  | sex  | id | name   | interest  | score |
+----+--------+------+------+----+--------+-----------+-------+
|  1 | 小王   | 15   | 男   |  1 | 小王   | 打篮球    | 81    |
|  2 | 小李   | 18   | 男   |  2 | 小李   | 踢足球    | 84    |
|  3 | 小红   | 16   | 女   |  3 | 小红   | 看动漫    | 99    |
+----+--------+------+------+----+--------+-----------+-------+
3 rows in set (0.00 sec)

从上面的截图可以很容易看出来三者之间的区别,left join 查出来的数据条数是以查询的表为主,right join查出来的数据条数是以关联表为主,inner join查出来的数据是在查询条件在两者的交集


(转) mysql中left join,right join,inner join的区别的更多相关文章

  1. MySQL 中 key, primary key ,unique key,index的区别

    一.key与primary key区别 CREATE TABLE wh_logrecord ( logrecord_id int(11) NOT NULL auto_increment, user_n ...

  2. 【转】Mysql中的排序规则utf8_unicode_ci、utf8_general_ci的区别总结

    Mysql中utf8_general_ci与utf8_unicode_ci有什么区别呢?在编程语言中,通常用unicode对中文字符做处理,防止出现乱码,那么在MySQL里,为什么大家都使用utf8_ ...

  3. mysql中时间类型datetime,timestamp与int的区别

    在mysql中存储时间,我们可以用datetime 格式,timestamp格式,也可以用int格式.那么我们设计的时候该如何考虑呢? 首先,我觉得应该明白这几个格式究竟是如何的,然后看看他们的区别, ...

  4. Mysql中的排序规则utf8_unicode_ci、utf8_general_ci的区别总结

    Mysql中utf8_general_ci与utf8_unicode_ci有什么区别呢?在编程语言中,通常用unicode对中文字符做处理,防止出现乱码,那么在MySQL里,为什么大家都使用utf8_ ...

  5. MYSQL中的普通索引,主健,唯一,全文索引区别

    MYSQL索引用来快速地寻找那些具有特定值的记录,所有MySQL索引都以B-树的形式保存.如果没有索引,执行查询时MySQL必须从第一个记录开始扫描整个表的所有记录,直至找到符合要求的记录.表里面的记 ...

  6. MySql中的count、NULL和空串的区别

    **1.count (1).count (*) 与 count (列名) 的区别** 表 count(1) count(*) count (列名) 作用 统计表中的所有的记录数 会统计表中的所有的记录 ...

  7. 浅谈SQL Server、MySQL中char,varchar,nchar,nvarchar区别

    最近一次的面试中,被面试官问到varchar和nvarchar的区别,脑海里记得是定长和可变长度的区别,但却没能说出来.后来,在网上找了下网友总结的区别.在这里做个备忘录: 一,SQL Server中 ...

  8. MySQL中where和on,where和having 的区别

    where和on的区别 用到连接查询时on会常用到,我们以左连接为例,来了解on的作用. on是在生成临时表使用的条件,不管on子句的条件是否为真,其都会返回左表的数据,如果条件为真则右表对应的数据也 ...

  9. mysql中char,varchar与text类型的区别和选用

    关于char,varchar与text平时没有太在意,一般来说,可能现在大家都是用varchar.但是当要存储的内容比较大时,究竟是选择varchar还是text呢?不知道...... 于是去查阅了一 ...

  10. mysql中几个日期时间类型之间的区别和使用

    MySQL中有如下几个时间类型:date.time.datetime.timestamp.year MySQL数据类型           含义 date                     只存 ...

随机推荐

  1. 从源码看Spring Boot 2.0.1

    Spring Boot 命名配置很少,却可以做到和其他配置复杂的框架相同的功能工作,从源码来看是怎么做到的. 我这里使用的Spring Boot版本是 2.0.1.RELEASE Spring Boo ...

  2. 7,EasyNetQ-控制队列名称

    EasyNetQ在为队列生成名称时的默认行为是使用   消息类型名称+subscription Id 例如,名称空间EasyNetQ.Tests.Integration中的PartyInvitatio ...

  3. 模拟页面获取的php数据(一)

    <?php return array( "aData" => array(//通勤方式 "trafficType" => array( 0 = ...

  4. 牛客国庆集训派对Day4 Solution

    A    深度学习 puts(n) #include <bits/stdc++.h> using namespace std; int main() { double n; while ( ...

  5. [洛谷U40581]树上统计treecnt

    [洛谷U40581]树上统计treecnt 题目大意: 给定一棵\(n(n\le10^5)\)个点的树. 定义\(Tree[l,r]\)表示为了使得\(l\sim r\)号点两两连通,最少需要选择的边 ...

  6. 解决qt提示:qt.network.ssl: QSslSocket: cannot call unresolved function DH_free

    方法一(解决):把C:\Qt\Qt5.8.0\Tools\QtCreator\bin下的libeay32.dll和ssleay32.dll库复制到C:\Qt\Qt5.8.0\5.8\msvc2015_ ...

  7. JAVA4种线程池的使用

    Java通过Executors提供四种线程池,分别为:newCachedThreadPool创建一个可缓存线程池,如果线程池长度超过处理需要,可灵活回收空闲线程,若无可回收,则新建线程.newFixe ...

  8. Java中的语法糖

    一.范型 1. C#和Java范型的区别 在C#中范型是切实存在的,List<int>和List<String>就是两种不同的类型,它们在系统运行期间生成,有自己的虚方法表和类 ...

  9. Installing Windows Features without Internet

    To set the source file path for the sxs folder, complete these steps: Ensure you have the correct in ...

  10. ReactNative: 搭建ReactNative开发环境

    搭建ReactNative开发环境 不废话,具体步骤如下: 一.安装需要的软件 1.Homebrew Homebrew, Mac系统的包管理器,用于安装NodeJS和一些其他必需的工具软件. /usr ...