• SQL语句中 LEFT JOIN 后 ON 和 WHERE 的区别


    阐述

    写SQL时本想通过 A left B join on and 后面的条件查出的两条记录变成一条,奈何发现还是有两条。

    后来发现 join on and 不会过滤结果记录条数,只会根据 and 后的条件是否显示 B 表的记录,A 表的记录一定会显示。

    不管 and 后面的是 A.id=1 还是 B.id=1,都显示出 A 表中所有的记录,并关联显示 B 中对应 A 表中 id1 的记录或者B表中 id 为 1 的记录。

    在这里插入图片描述

    SELECT u.id,u.nickname,c.content FROM chat_user u
    LEFT JOIN chat_communication c
    on u.id=c.toid order by c.id
    
    • 1
    • 2
    • 3
    +----+------------------+--------------+
    | id | nickname         | content      |
    +----+------------------+--------------+
    | 86 | 大美如斯         | NULL         |
    | 88 | 悦悦             | NULL         |
    | 89 | 雨薇             | NULL         |
    | 87 | 大金             | 你好         |
    | 85 | Love violet life | 你也好       |
    | 87 | 大金             | 你在干嘛     |
    | 87 | 大金             | 你还在吗     |
    | 87 | 大金             | 订单         |
    +----+------------------+--------------+
    8 rows in set (0.00 sec)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    SELECT u.id,u.nickname,c.content FROM chat_user u
    LEFT JOIN chat_communication c
    on u.id=c.toid and u.nickname="大金" order by c.id
    
    +----+------------------+--------------+
    | id | nickname         | content      |
    +----+------------------+--------------+
    | 85 | Love violet life | NULL         |
    | 86 | 大美如斯         | NULL         |
    | 88 | 悦悦             | NULL         |
    | 89 | 雨薇             | NULL         |
    | 87 | 大金             | 你好         |
    | 87 | 大金             | 你在干嘛     |
    | 87 | 大金             | 你还在吗     |
    | 87 | 大金             | 订单         |
    +----+------------------+--------------+
    8 rows in set (0.00 sec)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    SELECT u.id,u.nickname,c.content,c.fromname FROM chat_user u
    LEFT JOIN chat_communication c
    on u.id=c.toid and c.fromname="雨薇" order by c.id
    
    • 1
    • 2
    • 3

    在这里插入图片描述
    数据库在通过连接两张或多张表来返回记录时,都会生成一张中间的临时表,然后再将这张临时表返回给用户。

    left join 的 on 和 where 的条件区别

    1、 on 条件是在生成临时表时使用的条件,它不管 on 中的条件是否为真,都会返回左边表中的记录。

    2、where 条件是在临时表生成好后,再对临时表进行过滤的条件。这时已经没有 left join 的含义(必须返回左边表的记录)了,条件不为真的就全部过滤掉。

    新建两张表:

    CREATE TABLE test1(id int,size int); 
    INSERT INTO test1 VALUES (1,10),(2,20),(3,30); 
    
    CREATE TABLE test2(size int,nikname varchar(10));
    INSERT INTO test2 VALUES (10,"AAA"),(20,"BBB"),(20,"CCC"); 
    
    • 1
    • 2
    • 3
    • 4
    • 5

    表1:test1

    mysql> select * from test1;
    +------+------+
    | id   | size |
    +------+------+
    |    1 |   10 |
    |    2 |   20 |
    |    3 |   30 |
    +------+------+
    3 rows in set (0.00 sec)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    表2:test2

    +------+---------+
    | size | nikname |
    +------+---------+
    |   10 | AAA     |
    |   20 | BBB     |
    |   20 | CCC     |
    +------+---------+
    3 rows in set (0.01 sec)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    第一条 SQL

    SELECT * FROM test1
    LEFT JOIN test2 ON (test1.size = test2.size)
    WHERE test2.nikname = "AAA";
    
    +------+------+------+---------+
    | id   | size | size | nikname |
    +------+------+------+---------+
    |    1 |   10 |   10 | AAA     |
    +------+------+------+---------+
    1 row in set (0.00 sec)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    第二条 SQL

    select * FROM test1 left join test2 
    on (test1.size = test2.size and test2.nikname="AAA");
    
    +------+------+------+---------+
    | id   | size | size | nikname |
    +------+------+------+---------+
    |    1 |   10 |   10 | AAA     |
    |    2 |   20 | NULL | NULL    |
    |    3 |   30 | NULL | NULL    |
    +------+------+------+---------+
    3 rows in set (0.00 sec)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    第一条SQL的过程

    1、中间表 on 条件 : test1.size = test2.size

    +------+------+------+---------+
    | id   | size | size | nikname |
    +------+------+------+---------+
    |    1 |   10 |   10 | AAA     |
    |    2 |   20 |   20 | BBB     |
    |    2 |   20 |   20 | CCC     |
    |    3 |   30 | NULL | NULL    |
    +------+------+------+---------+
    4 rows in set (0.00 sec)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    2、再对中间表过滤 where 条件:test2.nikname="AAA"

    +------+------+------+---------+
    | id   | size | size | nikname |
    +------+------+------+---------+
    |    1 |   10 |   10 | AAA     |
    +------+------+------+---------+
    1 row in set (0.00 sec)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    第二条SQL的过程

    1、中间表on条件:test1.size = test2.size and test2.nikname=’AAA’

    (条件不为真也会返回左表中的记录)

    +------+------+------+---------+
    | id   | size | size | nikname |
    +------+------+------+---------+
    |    1 |   10 |   10 | AAA     |
    |    2 |   20 | NULL | NULL    |
    |    3 |   30 | NULL | NULL    |
    +------+------+------+---------+
    3 rows in set (0.00 sec)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    其实以上结果的关键原因就是 left join,right join,full join 的特殊性,不管 on 上的条件是否为真都会返回 left 或 right 表中的记录。

    full 则具有 left 和 right 的特性的并集

    而 inner join 没这个特殊性,则条件放在 on 中和 where 中,返回的结果集是相同的。

  • 相关阅读:
    java学习day31(redis7)事务
    onlyoffice的介绍搭建、集成过程。Windows、Linux
    Hive【Hive(三)查询语句】
    Linux Kernel入门到精通系列讲解(QEMU-虚拟化篇) 2.5 Qemu实现RTC设备
    Linux网络编程系列之UDP组播
    图神经网络关系抽取论文阅读笔记(六)
    Unity 3D 物体的Inspector面板
    计算机中实数的比较
    深度学习理论:Categorical crossentropy 损失函数
    【654. 最大二叉树】
  • 原文地址:https://blog.csdn.net/weiguang102/article/details/127873781