引言mysql
內外聯結的區別是內聯結將去除全部不符合條件的記錄,而外聯結則保留其中部分。外左聯結與外右聯結的區別在於若是用A左聯結B則A中全部記錄都會保留在結果中,此時B中只有符合聯結條件的記錄,而右聯結相反,這樣也就不會混淆了。sql
一.Join語法概述數據庫
join 用於多表中字段之間的聯繫,語法以下:性能優化
代碼以下:性能
FROM table1 INNER|LEFT|RIGHT JOIN table2 ON conditiona
table1:左表;table2:右表。測試
INNER JOIN(內鏈接,或等值鏈接):取得兩個表中存在鏈接匹配關係的記錄。優化
LEFT JOIN(左鏈接):取得左表(table1)徹底記錄,即便右表(table2)並沒有對應匹配記錄。spa
RIGHT JOIN(右鏈接):與 LEFT JOIN 相反,取得右表(table2)徹底記錄,即便左表(table1)並沒有匹配對應記錄。ci
注意:mysql不支持Full join,不過能夠經過UNION 關鍵字來合併 LEFT JOIN 與 RIGHT JOIN來模擬FULL join.it
接下來給出一個列子用於解釋下面幾種分類。以下兩個表(A,B)
代碼以下:
mysql> select A.id,A.name,B.name from A,B where A.id=B.id;//和內鏈接等價 +----+-----------+-------------+ | id | name | name | +----+-----------+-------------+ | 1 | Pirate | Rutabaga | | 2 | Monkey | Pirate | | 3 | Ninja | Darth Vader | | 4 | Spaghetti | Ninja | +----+-----------+-------------+ 4 rows in set (0.00 sec)
內鏈接,也叫等值鏈接,inner join產生同時符合A和B的一組數據。
代碼以下:
mysql> select * from A inner join B on A.name = B.name; +----+--------+----+--------+ | id | name | id | name | +----+--------+----+--------+ | 1 | Pirate | 2 | Pirate | | 3 | Ninja | 4 | Ninja | +----+--------+----+--------+
代碼以下:
mysql> select * from A left join B on A.name = B.name; #或者:select * from A left outer join B on A.name = B.name; +----+-----------+------+--------+ | id | name | id | name | +----+-----------+------+--------+ | 1 | Pirate | 2 | Pirate | | 2 | Monkey | NULL | NULL | | 3 | Ninja | 4 | Ninja | | 4 | Spaghetti | NULL | NULL | +----+-----------+------+--------+ 4 rows in set (0.00 sec)
left join,(或left outer join:在Mysql中二者等價,推薦使用left join.)左鏈接從左表(A)產生一套完整的記錄,與匹配的記錄(右表(B)) .若是沒有匹配,右側將包含null。
若是想只從左表(A)中產生一套記錄,但不包含右表(B)的記錄,能夠經過設置where語句來執行,以下:
代碼以下:
mysql> select * from A left join B on A.name=B.name where A.id is null or B.id is null;//where A.id is null能夠不寫,由於是左鏈接,那麼A.id 確定是有值的 +----+-----------+------+------+ | id | name | id | name | +----+-----------+------+------+ | 2 | Monkey | NULL | NULL | | 4 | Spaghetti | NULL | NULL | +----+-----------+------+------+ 2 rows in set (0.00 sec)
同理,還能夠模擬inner join. 以下:
代碼以下:
mysql> select * from A left join B on A.name=B.name where A.id is not null and B.id is not null; +----+--------+------+--------+ | id | name | id | name | +----+--------+------+--------+ | 1 | Pirate | 2 | Pirate | | 3 | Ninja | 4 | Ninja | +----+--------+------+--------+ 2 rows in set (0.00 sec)
求差集:
根據上面的例子能夠求差集,以下:
代碼以下:
SELECT * FROM A LEFT JOIN B ON A.name = B.name WHERE B.id IS NULL union SELECT * FROM A right JOIN B ON A.name = B.name WHERE A.id IS NULL; # 結果 +------+-----------+------+-------------+ | id | name | id | name | +------+-----------+------+-------------+ | 2 | Monkey | NULL | NULL | | 4 | Spaghetti | NULL | NULL | | NULL | NULL | 1 | Rutabaga | | NULL | NULL | 3 | Darth Vader | +------+-----------+------+-------------+
四.Right join
代碼以下:
mysql> select * from A right join B on A.name = B.name; +------+--------+----+-------------+ | id | name | id | name | +------+--------+----+-------------+ | NULL | NULL | 1 | Rutabaga | | 1 | Pirate | 2 | Pirate | | NULL | NULL | 3 | Darth Vader | | 3 | Ninja | 4 | Ninja | +------+--------+----+-------------+ 4 rows in set (0.00 sec)
同left 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 A cross join B; +----+-----------+----+-------------+ | id | name | id | name | +----+-----------+----+-------------+ | 1 | Pirate | 1 | Rutabaga | | 2 | Monkey | 1 | Rutabaga | | 3 | Ninja | 1 | Rutabaga | | 4 | Spaghetti | 1 | Rutabaga | | 1 | Pirate | 2 | Pirate | | 2 | Monkey | 2 | Pirate | | 3 | Ninja | 2 | Pirate | | 4 | Spaghetti | 2 | Pirate | | 1 | Pirate | 3 | Darth Vader | | 2 | Monkey | 3 | Darth Vader | | 3 | Ninja | 3 | Darth Vader | | 4 | Spaghetti | 3 | Darth Vader | | 1 | Pirate | 4 | Ninja | | 2 | Monkey | 4 | Ninja | | 3 | Ninja | 4 | Ninja | | 4 | Spaghetti | 4 | Ninja | +----+-----------+----+-------------+ 16 rows in set (0.00 sec)
#再執行:mysql> select * from A inner join B; 試一試 #在執行mysql> select * from A cross join B on A.name = B.name; 試一試
實際上,在 MySQL 中(僅限於 MySQL) CROSS JOIN 與 INNER JOIN 的表現是同樣的,在不指定 ON 條件獲得的結果都是笛卡爾積,反之取得兩個表徹底匹配的結果。 INNER JOIN 與 CROSS JOIN 能夠省略 INNER 或 CROSS 關鍵字,所以下面的 SQL 效果是同樣的:
代碼以下:
... FROM table1 INNER JOIN table2 ... FROM table1 CROSS JOIN table2 ... FROM table1 JOIN table2
六.Full join
代碼以下:
mysql> select * from A left join B on B.name = A.name -> union -> select * from A right join B on B.name = A.name; +------+-----------+------+-------------+ | id | name | id | name | +------+-----------+------+-------------+ | 1 | Pirate | 2 | Pirate | | 2 | Monkey | NULL | NULL | | 3 | Ninja | 4 | Ninja | | 4 | Spaghetti | NULL | NULL | | NULL | NULL | 1 | Rutabaga | | NULL | NULL | 3 | Darth Vader | +------+-----------+------+-------------+ 6 rows in set (0.00 sec)
全鏈接產生的全部記錄(雙方匹配記錄)在表A和表B。若是沒有匹配,則對面將包含null。
如:
代碼以下:
select * from table a inner join table b on a.id = b.id;
VS
代碼以下:
select a.*, b.* from table a, table b where a.id = b.id;
我在數據庫中比較(10w數據)得之,它們用時幾乎相同,第一個是顯示的inner join,後一個是隱式的inner join。
儘可能用inner join.避免 LEFT JOIN 和 NULL.
在使用left join(或right join)時,應該清楚的知道如下幾點:
ON 條件(「A LEFT JOIN B ON 條件表達式」中的ON)用來決定如何從 B 表中檢索數據行。若是 B 表中沒有任何一行數據匹配 ON 的條件,將會額外生成一行全部列爲 NULL 的數據,在匹配階段 WHERE 子句的條件都不會被使用。僅在匹配階段完成之後,WHERE 子句條件纔會被使用。ON將從匹配階段產生的數據中檢索過濾。
因此咱們要注意:在使用Left (right) join的時候,必定要在先給出儘量多的匹配知足條件,減小Where的執行。如:
PASS
代碼以下:
select * from A inner join B on B.name = A.name left join C on C.name = B.name left join D on D.id = C.id where C.status>1 and D.status=1;
Great
代碼以下:
select * from A inner join B on B.name = A.name left join C on C.name = B.name and C.status>1 left join D on D.id = C.id and D.status=1
從上面例子能夠看出,儘量知足ON的條件,而少用Where的條件。從執行性能來看第二個顯然更加省時。
如做者舉了一個列子:
代碼以下:
mysql> SELECT * FROM product LEFT JOIN product_details ON (product.id = product_details.id) AND product_details.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 | +----+--------+------+--------+-------+ 4 rows in set (0.00 sec) mysql> SELECT * FROM product LEFT JOIN product_details ON (product.id = product_details.id) WHERE product_details.id=2; +----+--------+----+--------+-------+ | id | amount | id | weight | exist | +----+--------+----+--------+-------+ | 2 | 200 | 2 | 22 | 0 | +----+--------+----+--------+-------+ 1 row in set (0.01 sec)
從上可知,第一條查詢使用 ON 條件決定了從 LEFT JOIN的 product_details表中檢索符合的全部數據行。第二條查詢作了簡單的LEFT JOIN,而後使用 WHERE 子句從 LEFT JOIN的數據中過濾掉不符合條件的數據行。
2.3 儘可能避免子查詢,而用join
exists和not exists 語法能夠理解爲:將主查詢的數據,放到子查詢中作條件驗證,根據驗證結果(TRUE 或 FALSE)來決定主查詢的數據結果是否得以保留
每每性能這玩意兒,更多時候體如今數據量比較大的時候,此時,咱們應該避免複雜的子查詢。以下:
PASS
代碼以下:
insert into t1(a1) select b1 from t2 where not exists(select 1 from t1 where t1.id = t2.r_id);
Great
代碼以下:
insert into t1(a1) select b1 from t2 left join (select distinct t1.id from t1 ) t1 on t1.id = t2.r_id where t1.id is null;
現有以下2個保存學校班級比賽的表,一個表保存班級id和班級名,另一個表保存主場班級id,客場班級id和比賽場數,如今須要鏈接查詢生成主場班級名,客場班級名,比賽場數的表,求具體實現的sql語句.
mysql> select * from class; +------+--------+ | c_id | c_name | +------+--------+ | 1 | 1班 | | 2 | 2班 | | 3 | 3班 | | 4 | 4班 | +------+--------+ 4 rows in set (0.00 sec)
mysql> select * from team; +------+------+------+ | h_id | g_id | num | +------+------+------+ | 1 | 2 | 34 | | 2 | 4 | 37 | +------+------+------+ 2 rows in set (0.00 sec)
發現要生成2個班級名的表,而一次join鏈接能夠生成一個,則須要2次join鏈接,因爲class表和team錶鏈接內容必需要匹配,因此採用內鏈接.
mysql> select ct.h_name,class.c_name as g_name,ct.num from (select class.c_name as h_name,team.* from class join team on class.c_id=team.h_id) as ct join class on ct.g_id=class.c_id; +--------+--------+------+ | h_name | g_name | num | +--------+--------+------+ | 1班 | 2班 | 34 | | 2班 | 4班 | 37 | +--------+--------+------+ 2 rows in set (0.00 sec)