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都會對生成的臨時表進行過濾

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM