zoukankan      html  css  js  c++  java
  • 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都会对生成的临时表进行过滤

  • 相关阅读:
    CF979D Kuro and GCD and XOR and SUM(01Trie)
    2020中国计量大学校赛题解
    CF16E Fish (状压dp)
    2017ccpc杭州站题解
    HDU6274 Master of Sequence(二分+预处理)
    CF899F Letters Removing(树状数组+二分)
    牛客 tokitsukaze and Soldier(优先队列+排序)
    HDU6268 Master of Subgraph(点分治)
    CF862E Mahmoud and Ehab and the function(二分)
    CF1108F MST Unification(生成树+思维)
  • 原文地址:https://www.cnblogs.com/HKUI/p/8536969.html
Copyright © 2011-2022 走看看