1. 程式人生 > 其它 >mysql left join、right join、inner join用法分析

mysql left join、right join、inner join用法分析

四種聯接

left join(左聯接)

返回包括左表中的所有記錄和右表中聯結欄位相等的記錄

right join(右聯接)

返回包括右表中的所有記錄和左表中聯結欄位相等的記錄

inner join(等值聯接)

只返回兩個表中聯結欄位相等的行

cross join(交叉聯接)

得到的結果是兩個表的乘積,即笛卡爾積

建立表
CREATE TABLE `product` (`id` int(10) unsigned not null auto_increment,`amount` int(10) unsigned default null,PRIMARY KEY  (`id`)) ENGINE=innodb;
CREATE TABLE `product_details` (`id` int(10) unsigned not null,`weight` int(10) unsigned default null,`exist` int(10) unsigned default null,PRIMARY KEY  (`id`)) ENGINE=innodb;

插入資料

INSERT INTO product (id,amount) VALUES (1,100),(2,200),(3,300),(4,400);
INSERT INTO product_details (id,weight,exist) VALUES (2,22,0),(4,44,1),(5,55,0),(6,66,1);
mysql>   SELECT * FROM product;
+----+--------+
| id | amount |
+----+--------+
|  1 |    100 |
|  2 |    200 |
|  3 |    300 |
|  4 |    400 |
+----+--------+
4 rows in set (0.00 sec)
mysql> SELECT * FROM product_details;
+----+--------+-------+
| id | weight | exist |
+----+--------+-------+
|  2 |     22 |       0 |
|  4 |     44 |       1 |
|  5 |     55 |       0 |
|  6 |     66 |       1 |
+----+--------+-------+
4 rows in set (0.00 sec)

inner join(等值聯接)

mysql> select * from product a inner join product_details b on a.id=b.id;
+----+--------+----+--------+-------+ 
| id | amount | id | weight | exist | 
+----+--------+----+--------+-------+   
|  2 |    200 |    2 |     22 |     0 |  
|  4 |    400 |    4 |     44 |     1 |   
 +----+--------+----+--------+-------+ 
 2 rows in set (0.00 sec)

left join(左聯接)

mysql>   select * from product a left join product_details b on a.id=b.id;
+----+--------+------+--------+-------+
| id | amount | id   | weight | exist   |
+----+--------+------+--------+-------+
|  1 |    100 | NULL |   NULL |    NULL |
|  2 |    200 |      2 |     22 |     0 |
|  3 |    300 | NULL |   NULL |    NULL |
|  4 |    400 |      4 |     44 |     1 |
+----+--------+------+--------+-------+
mysql> select * from product a left join product_details b on a.id=b.id and b.id=2;
+----+--------+------+--------+-------+
| id | amount | id   | weight | exist   |
+----+--------+------+--------+-------+
|  1 |    100 | NULL |   NULL |    NULL |
|  2 |    200 |      2 |     22 |     0 |
|  3 |    300 | NULL |   NULL |    NULL |
|  4 |    400 | NULL |   NULL |    NULL |
+----+--------+------+--------+-------+     
mysql> select * from product a left join product_details b on a.id=b.id where b.id=2; 
+----+--------+----+--------+-------+
| id | amount | id | weight | exist |
+----+--------+----+--------+-------+
|  2 |    200 |    2 |     22 |     0 |
+----+--------+----+--------+-------+  
mysql> select * from product a left join product_details b on a.id=b.id where a.id=3; 
+----+--------+------+--------+-------+
| id | amount | id   | weight | exist|
+----+--------+------+--------+-------+
|  3 |    300 | NULL |   NULL |    NULL |
+----+--------+------+--------+-------+     
mysql> select * from product a left join product_details b on a.id=b.id   and a.id=3;  
+----+--------+------+--------+-------+
| id | amount | id   | weight | exist   |
+----+--------+------+--------+-------+
|  1 |    100 | NULL |   NULL |    NULL |
|  2 |    200 | NULL |   NULL |    NULL |
|  3 |    300 | NULL |   NULL |    NULL |
|  4 |    400 | NULL |   NULL |    NULL |
+----+--------+------+--------+-------+     
mysql> SELECT * FROM product a LEFT JOIN product_details b ON a.id=b.id   AND b.weight!=44 AND b.exist=0 WHERE b.id IS NULL;  
+----+--------+------+--------+-------+
| id | amount | id   | weight | exist   |
+----+--------+------+--------+-------+
|  1 |    100 | NULL |   NULL |    NULL |
|  3 |    300 | NULL |   NULL |    NULL |
|  4 |    400 | NULL |   NULL |    NULL |
+----+--------+------+--------+-------+     
mysql> SELECT * FROM product a LEFT JOIN product_details b ON a.id=b.id   AND b.weight!=44 AND b.exist=0 WHERE b.id IS not NULL;
+----+--------+------+--------+-------+
| id | amount | id   | weight | exist   |
+----+--------+------+--------+-------+
|  2 |    200 |      2 |     22 |     0 |
+----+--------+------+--------+-------+

right join跟left join相反,不多做解釋,MySQL本身不支援所說的full join(全連線),但可以通過union來實現。

mysql>   select * from product a left join product_details b on a.id=b.id;
+----+--------+------+--------+-------+
| id | amount | id   | weight | exist   |
+----+--------+------+--------+-------+
|  1 |    100 | NULL |   NULL |    NULL |
|  2 |    200 |      2 |     22 |     0 |
|  3 |    300 | NULL |   NULL |    NULL |
|  4 |    400 |      4 |     44 |     1 |
+----+--------+------+--------+-------+
mysql> select * from product a left join product_details b on a.id=b.id and b.id=2;
+----+--------+------+--------+-------+
| id | amount | id   | weight | exist   |
+----+--------+------+--------+-------+
|  1 |    100 | NULL |   NULL |    NULL |
|  2 |    200 |      2 |     22 |     0 |
|  3 |    300 | NULL |   NULL |    NULL |
|  4 |    400 | NULL |   NULL |    NULL |
+----+--------+------+--------+-------+ 
mysql> select * from product a left join product_details b on a.id=b.id where b.id=2; 
+----+--------+----+--------+-------+
| id | amount | id | weight | exist |
+----+--------+----+--------+-------+
|  2 |    200 |    2 |     22 |     0 |
+----+--------+----+--------+-------+  
mysql> select * from product a left join product_details b on a.id=b.id where a.id=3; 
+----+--------+------+--------+-------+
| id | amount | id   | weight | exist|
+----+--------+------+--------+-------+
|  3 |    300 | NULL |   NULL |    NULL |
+----+--------+------+--------+-------+     
mysql> select * from product a left join product_details b on a.id=b.id   and a.id=3;  
+----+--------+------+--------+-------+
| id | amount | id   | weight | exist   |
+----+--------+------+--------+-------+
|  1 |    100 | NULL |   NULL |    NULL |
|  2 |    200 | NULL |   NULL |    NULL |
|  3 |    300 | NULL |   NULL |    NULL |
|  4 |    400 | NULL |   NULL |    NULL |
+----+--------+------+--------+-------+     
mysql> SELECT * FROM product a LEFT JOIN product_details b ON a.id=b.id   AND b.weight!=44 AND b.exist=0 WHERE b.id IS NULL;  
+----+--------+------+--------+-------+
| id | amount | id   | weight | exist   |
+----+--------+------+--------+-------+
|  1 |    100 | NULL |   NULL |    NULL |
|  3 |    300 | NULL |   NULL |    NULL |
|  4 |    400 | NULL |   NULL |    NULL |
+----+--------+------+--------+-------+     
mysql> SELECT * FROM product a LEFT JOIN product_details b ON a.id=b.id   AND b.weight!=44 AND b.exist=0 WHERE b.id IS not NULL;
+----+--------+------+--------+-------+
| id | amount | id   | weight | exist   |
+----+--------+------+--------+-------+
|  2 |    200 |      2 |     22 |     0 |
+----+--------+------+--------+-------+

Cross join(交叉聯接)

cross join:交叉聯接,得到的結果是兩個表的乘積,即笛卡爾積。

笛卡爾(Descartes)乘積又叫直積。

假設集合A={a,b},集合B={0,1,2},則兩個集合的笛卡爾積為{(a,0),(a,1),(a,2),(b,0),(b,1), (b,2)}。可以擴充套件到多個集合的情況。

類似的例子有,如果A表示某學校學生的集合,B表示該學校所有課程的集合,則A與B的笛卡爾積表示所有可能的選課情況。

mysql>  select * from product a cross join   product_details b;
+----+--------+----+--------+-------+
| id | amount | id | weight | exist |
+----+--------+----+--------+-------+
|  1 |    100 |    2 |     22 |     0 |
|  2 |    200 |    2 |     22 |     0 |
|  3 |    300 |    2 |     22 |     0 |
|  4 |    400 |    2 |     22 |     0 |
|  1 |    100 |    4 |     44 |     1 |
|  2 |    200 |    4 |     44 |     1 |
|  3 |    300 |    4 |     44 |     1 |
|  4 |    400 |    4 |     44 |     1 |
|  1 |    100 |    5 |     55 |     0 |
|  2 |    200 |    5 |     55 |     0 |
|  3 |    300 |    5 |     55 |     0 |
|  4 |    400 |    5 |     55 |     0 |
|  1 |    100 |    6 |     66 |     1 |
|  2 |    200 |    6 |     66 |     1 |
|  3 |    300 |    6 |     66 |     1 |
|  4 |    400 |    6 |     66 |     1 |
+----+--------+----+--------+-------+

on與 where的執行順序

ON 條件(“A LEFT JOIN B ON 條件表示式”中的ON)用來決定如何從 B 表中檢索資料行。如果 B 表中沒有任何一行資料匹配 ON 的條件,將會額外生成一行所有列為 NULL 的資料,在匹配階段 WHERE 子句的條件都不會被使用。僅在匹配階段完成以後,WHERE 子句條件才會被使用。它將從匹配階段產生的資料中檢索過濾。

所以我們要注意:在使用Left (right) join的時候,一定要在先給出儘可能多的匹配滿足條件,減少Where的執行。

A Left join B On a.id=b.idAnd b.id=2;從B表中檢索符合的所有資料行,如果沒有匹配的全部為null A Left join B On a.id=b.idWhere b.id=2;先做left join 再過濾, WHERE 條件查詢發生在匹配階段之後