• MYSQL 表左连接 ON AND 和ON WHERE 的区别


    首先是针对左右连接,这里与inner join区分

    在使用left join时,on and 和on where会有区别

    1. on的条件是在连接生成临时表时使用的条件,以左表为基准 ,不管on中的条件真否,都会返回左表中的记录
    2.where条件是在临时表生成好后,再对临时表过滤。此时 和left join有区别(返回左表全部记录),条件不为真就全部过滤掉,on后的条件来生成左右表关联的临时表,
    where后的条件是生成临时表后对临时表过滤

    on and是进行韦恩运算时 连接时就做的动作,where是全部连接完后,再根据条件过滤

    CREATE TABLE `a` (
      `id` int(11) NOT NULL AUTO_INCREMENT,
      `sid` int(11) NOT NULL,
      `type` char(10) NOT NULL,
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB  DEFAULT CHARSET=utf8;
    
    INSERT INTO `a` (`id`, `sid`, `type`) VALUES (1, 1, 'a');
    INSERT INTO `a` (`id`, `sid`, `type`) VALUES (2, 1, 'b');
    INSERT INTO `a` (`id`, `sid`, `type`) VALUES (3, 2, 'c');
    INSERT INTO `a` (`id`, `sid`, `type`) VALUES (4, 3, 'd');
    
    CREATE TABLE `b` (
      `sid` int(11) NOT NULL,
      `remark` char(10) DEFAULT NULL
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
    INSERT INTO `b` (`sid`, `remark`) VALUES (1, 'A');
    INSERT INTO `b` (`sid`, `remark`) VALUES (2, 'B');
    INSERT INTO `b` (`sid`, `remark`) VALUES (3, 'C');
    INSERT INTO `b` (`sid`, `remark`) VALUES (4, 'D');
    mysql> select * from a;
    +----+-----+------+
    | id | sid | type |
    +----+-----+------+
    |  1 |   1 | a    |
    |  2 |   1 | b    |
    |  3 |   2 | c    |
    |  4 |   3 | d    |
    +----+-----+------+
    4 rows in set
    
    mysql> select * from b;
    +-----+--------+
    | sid | remark |
    +-----+--------+
    |   1 | A      |
    |   2 | B      |
    |   3 | C      |
    |   4 | D      |
    +-----+--------+
    4 rows in set
    mysql> select * from a left join b on a.sid=b.sid;
    +----+-----+------+-----+--------+
    | id | sid | type | sid | remark |
    +----+-----+------+-----+--------+
    |  1 |   1 | a    |   1 | A      |
    |  2 |   1 | b    |   1 | A      |
    |  3 |   2 | c    |   2 | B      |
    |  4 |   3 | d    |   3 | C      |
    +----+-----+------+-----+--------+
    mysql> select * from a left join b on a.sid=b.sid and a.sid=1;
    +----+-----+------+------+--------+
    | id | sid | type | sid  | remark |
    +----+-----+------+------+--------+
    |  1 |   1 | a    |    1 | A      |
    |  2 |   1 | b    |    1 | A      |
    |  3 |   2 | c    | NULL | NULL   |
    |  4 |   3 | d    | NULL | NULL   |
    +----+-----+------+------+--------+
    mysql> select * from a left join b on a.sid=b.sid where a.sid=1;
    +----+-----+------+-----+--------+
    | id | sid | type | sid | remark |
    +----+-----+------+-----+--------+
    |  1 |   1 | a    |   1 | A      |
    |  2 |   1 | b    |   1 | A      |
    +----+-----+------+-----+--------+

    对于inner join

    mysql> select * from a inner join b on a.sid=b.sid and a.sid=1;
    +----+-----+------+-----+--------+
    | id | sid | type | sid | remark |
    +----+-----+------+-----+--------+
    |  1 |   1 | a    |   1 | A      |
    |  2 |   1 | b    |   1 | A      |
    +----+-----+------+-----+--------+
    mysql> select * from a inner join b on a.sid=b.sid where a.sid=1;
    +----+-----+------+-----+--------+
    | id | sid | type | sid | remark |
    +----+-----+------+-----+--------+
    |  1 |   1 | a    |   1 | A      |
    |  2 |   1 | b    |   1 | A      |
    +----+-----+------+-----+--------+

    on and和on where结果一致
    在使用inner join时,不管是对左表还是右表进行筛选,on and和on where都会对生成的临时表进行过滤

  • 相关阅读:
    Android项目实战(四):ViewPager切换动画(3.0版本以上有效果)
    安卓开发_浅谈ListView(SimpleAdapter数组适配器)
    ADB server didn't ACK 解决方法
    安卓开发_浅谈自定义组件
    Go语言基础之指针
    Go语言基础之接口
    Go语言标准库之fmt
    Go语言基础之函数
    LeetCode go
    Go语言基础之变量和常量
  • 原文地址:https://www.cnblogs.com/HKUI/p/8536969.html
Copyright © 2020-2023  润新知