一.Join語法概述
join 用于多表中字段之間的聯(lián)系,語法如下:
... FROM table1 INNER|LEFT|RIGHT JOIN table2 ON condition
table1:左表;table2:右表。
JOIN 按照功能大致分為如下三類:
INNER JOIN(內(nèi)連接,或等值連接):取得兩個(gè)表中存在連接匹配關(guān)系的記錄。
LEFT JOIN(左連接):取得左表(table1)完全記錄,即是右表(table2)并無對應(yīng)匹配記錄。
RIGHT JOIN(右連接):與 LEFT JOIN 相反,取得右表(table2)完全記錄,即是左表(table1)并無匹配對應(yīng)記錄。
注意:mysql不支持Full join,不過可以通過UNION 關(guān)鍵字來合并 LEFT JOIN 與 RIGHT JOIN來模擬FULL join.
案例分析:
1、案例環(huán)境
mysql> select * from emp; +-------+--------+-----------+------+------------+------+------+--------+ | empno | ENAME | JOB | MGR | HIRE | SAL | COMM | deptno | +-------+--------+-----------+------+------------+------+------+--------+ | 7369 | SMITH | CLERK | 7902 | 1980-12-17 | 800 | NULL | 20 | | 7499 | ALLEN | SALESMAN | 7698 | 1981-02-20 | 1600 | 300 | 30 | | 7521 | WARD | SALESMAN | 7698 | 1981-02-22 | 1250 | 500 | 30 | | 7566 | JONES | MANAGER | 7839 | 1981-04-02 | 2975 | NULL | 20 | | 7654 | MARTIN | SALESMAN | 7698 | 1981-09-28 | 1250 | 1400 | 30 | | 7698 | BLAKE | MANAGER | 7839 | 1981-05-01 | 2850 | NULL | 30 | | 7782 | CLARK | MANAGER | 7839 | 1981-06-09 | 2450 | NULL | 10 | | 7788 | SCOTT | ANALYST | 7566 | 1987-07-13 | 3000 | NULL | 20 | | 7839 | KING | PRESIDENT | NULL | 1981-11-17 | 5000 | NULL | 10 | | 7844 | TURNER | SALESMAN | 7698 | 1981-09-08 | 1500 | 0 | 30 | | 7876 | ADAMS | CLERK | 7788 | 1987-06-13 | 1100 | NULL | 20 | | 7900 | JAMES | CLERK | 7698 | 1981-12-03 | 950 | NULL | 30 | | 7902 | FORD | ANALYST | 7566 | 1981-12-03 | 3000 | NULL | 20 | | 7934 | MILLER | CLERK | 7782 | 1982-01-23 | 1300 | NULL | 10 | +-------+--------+-----------+------+------------+------+------+--------+ 14 rows in set (0.00 sec) mysql> select * from dept; +--------+------------+---------+ | deptNO | DNAME | LOC | +--------+------------+---------+ | 10 | ACCOUNTING | NEWYORK | | 20 | RESEARCH | DALLAS | | 30 | SALES | CHICAGO | | 40 | OPERATIONS | BOSTON | +--------+------------+---------+ 4 rows in set (0.00 sec)
inner join:(內(nèi)連接,或等值連接):取得兩個(gè)表中存在連接匹配關(guān)系的記錄。
mysql> select e.empno,e.ename,e.sal,e.deptno,d.dname -> from emp e -> inner join dept d -> where e.deptno=d.deptno; +-------+--------+------+--------+------------+ | empno | ename | sal | deptno | dname | +-------+--------+------+--------+------------+ | 7782 | CLARK | 2450 | 10 | ACCOUNTING | | 7839 | KING | 5000 | 10 | ACCOUNTING | | 7934 | MILLER | 1300 | 10 | ACCOUNTING | | 7369 | SMITH | 800 | 20 | RESEARCH | | 7566 | JONES | 2975 | 20 | RESEARCH | | 7788 | SCOTT | 3000 | 20 | RESEARCH | | 7876 | ADAMS | 1100 | 20 | RESEARCH | | 7902 | FORD | 3000 | 20 | RESEARCH | | 7499 | ALLEN | 1600 | 30 | SALES | | 7521 | WARD | 1250 | 30 | SALES | | 7654 | MARTIN | 1250 | 30 | SALES | | 7698 | BLAKE | 2850 | 30 | SALES | | 7844 | TURNER | 1500 | 30 | SALES | | 7900 | JAMES | 950 | 30 | SALES | +-------+--------+------+--------+------------+ 14 rows in set (0.00 sec) mysql> select e.empno,e.ename,e.sal,e.deptno,d.dname -> from emp e -> inner join dept d -> on e.deptno=d.deptno; +-------+--------+------+--------+------------+ | empno | ename | sal | deptno | dname | +-------+--------+------+--------+------------+ | 7782 | CLARK | 2450 | 10 | ACCOUNTING | | 7839 | KING | 5000 | 10 | ACCOUNTING | | 7934 | MILLER | 1300 | 10 | ACCOUNTING | | 7369 | SMITH | 800 | 20 | RESEARCH | | 7566 | JONES | 2975 | 20 | RESEARCH | | 7788 | SCOTT | 3000 | 20 | RESEARCH | | 7876 | ADAMS | 1100 | 20 | RESEARCH | | 7902 | FORD | 3000 | 20 | RESEARCH | | 7499 | ALLEN | 1600 | 30 | SALES | | 7521 | WARD | 1250 | 30 | SALES | | 7654 | MARTIN | 1250 | 30 | SALES | | 7698 | BLAKE | 2850 | 30 | SALES | | 7844 | TURNER | 1500 | 30 | SALES | | 7900 | JAMES | 950 | 30 | SALES | +-------+--------+------+--------+------------+ 14 rows in set (0.00 sec)
隱式內(nèi)連接:
mysql> select e.empno,e.ename,e.sal,e.deptno,d.dname -> from emp e,dept d -> where e.deptno=d.deptno; +-------+--------+------+--------+------------+ | empno | ename | sal | deptno | dname | +-------+--------+------+--------+------------+ | 7782 | CLARK | 2450 | 10 | ACCOUNTING | | 7839 | KING | 5000 | 10 | ACCOUNTING | | 7934 | MILLER | 1300 | 10 | ACCOUNTING | | 7369 | SMITH | 800 | 20 | RESEARCH | | 7566 | JONES | 2975 | 20 | RESEARCH | | 7788 | SCOTT | 3000 | 20 | RESEARCH | | 7876 | ADAMS | 1100 | 20 | RESEARCH | | 7902 | FORD | 3000 | 20 | RESEARCH | | 7499 | ALLEN | 1600 | 30 | SALES | | 7521 | WARD | 1250 | 30 | SALES | | 7654 | MARTIN | 1250 | 30 | SALES | | 7698 | BLAKE | 2850 | 30 | SALES | | 7844 | TURNER | 1500 | 30 | SALES | | 7900 | JAMES | 950 | 30 | SALES | +-------+--------+------+--------+------------+ 14 rows in set (0.00 sec)
left join:取得左表(table1)完全記錄,即是右表(table2)并無對應(yīng)匹配記錄。
mysql> update emp set deptno=null where empno=7788; Query OK, 1 row affected (0.07 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql> commit; Query OK, 0 rows affected (0.00 sec) mysql> select * from emp where empno=7788; +-------+-------+---------+------+------------+------+------+--------+ | empno | ENAME | JOB | MGR | HIRE | SAL | COMM | deptno | +-------+-------+---------+------+------------+------+------+--------+ | 7788 | SCOTT | ANALYST | 7566 | 1987-07-13 | 3000 | NULL | NULL | +-------+-------+---------+------+------------+------+------+--------+ 1 row in set (0.00 sec)
采用等值連接:
mysql> select e.empno,e.ename,e.sal,e.deptno,d.dname -> from emp e -> inner join dept d on e.deptno=d.deptno; +-------+--------+------+--------+------------+ | empno | ename | sal | deptno | dname | +-------+--------+------+--------+------------+ | 7782 | CLARK | 2450 | 10 | ACCOUNTING | | 7839 | KING | 5000 | 10 | ACCOUNTING | | 7934 | MILLER | 1300 | 10 | ACCOUNTING | | 7369 | SMITH | 800 | 20 | RESEARCH | | 7566 | JONES | 2975 | 20 | RESEARCH | | 7876 | ADAMS | 1100 | 20 | RESEARCH | | 7902 | FORD | 3000 | 20 | RESEARCH | | 7499 | ALLEN | 1600 | 30 | SALES | | 7521 | WARD | 1250 | 30 | SALES | | 7654 | MARTIN | 1250 | 30 | SALES | | 7698 | BLAKE | 2850 | 30 | SALES | | 7844 | TURNER | 1500 | 30 | SALES | | 7900 | JAMES | 950 | 30 | SALES | +-------+--------+------+--------+------------+ 13 rows in set (0.00 sec)
-----對于等值連接,只能看到條件匹配的記錄!
mysql> select e.empno,e.ename,e.sal,e.deptno,d.dname -> from emp e -> left join dept d on e.deptno=d.deptno; +-------+--------+------+--------+------------+ | empno | ename | sal | deptno | dname | +-------+--------+------+--------+------------+ | 7782 | CLARK | 2450 | 10 | ACCOUNTING | | 7839 | KING | 5000 | 10 | ACCOUNTING | | 7934 | MILLER | 1300 | 10 | ACCOUNTING | | 7369 | SMITH | 800 | 20 | RESEARCH | | 7566 | JONES | 2975 | 20 | RESEARCH | | 7876 | ADAMS | 1100 | 20 | RESEARCH | | 7902 | FORD | 3000 | 20 | RESEARCH | | 7499 | ALLEN | 1600 | 30 | SALES | | 7521 | WARD | 1250 | 30 | SALES | | 7654 | MARTIN | 1250 | 30 | SALES | | 7698 | BLAKE | 2850 | 30 | SALES | | 7844 | TURNER | 1500 | 30 | SALES | | 7900 | JAMES | 950 | 30 | SALES | | 7788 | SCOTT | 3000 | NULL | NULL | +-------+--------+------+--------+------------+ 14 rows in set (0.00 sec)
-----通過left join可以查看到emp表中不符合條件的記錄!
right join:與 LEFT JOIN 相反,取得右表(table2)完全記錄,即是左表(table1)并無匹配對應(yīng)記錄。
mysql> select e.empno,e.ename,e.sal,d.deptno,d.dname -> from emp e -> right join dept d on e.deptno=d.deptno; +-------+--------+------+--------+------------+ | empno | ename | sal | deptno | dname | +-------+--------+------+--------+------------+ | 7782 | CLARK | 2450 | 10 | ACCOUNTING | | 7839 | KING | 5000 | 10 | ACCOUNTING | | 7934 | MILLER | 1300 | 10 | ACCOUNTING | | 7369 | SMITH | 800 | 20 | RESEARCH | | 7566 | JONES | 2975 | 20 | RESEARCH | | 7876 | ADAMS | 1100 | 20 | RESEARCH | | 7902 | FORD | 3000 | 20 | RESEARCH | | 7499 | ALLEN | 1600 | 30 | SALES | | 7521 | WARD | 1250 | 30 | SALES | | 7654 | MARTIN | 1250 | 30 | SALES | | 7698 | BLAKE | 2850 | 30 | SALES | | 7844 | TURNER | 1500 | 30 | SALES | | 7900 | JAMES | 950 | 30 | SALES | | NULL | NULL | NULL | 40 | OPERATIONS | +-------+--------+------+--------+------------+ 14 rows in set (0.00 sec)
------查詢到dept表中,不符合條件的記錄?。?!
淺析Mysql Join語法以及性能優(yōu)化
在講MySQL的Join語法前還是先回顧一下聯(lián)結(jié)的語法,呵呵,其實(shí)連我自己都忘得差不多了,那就大家一起溫習(xí)吧,這里我有個(gè)比較簡便的記憶方法,內(nèi)外聯(lián)結(jié)的區(qū)別是內(nèi)聯(lián)結(jié)將去除所有不符合條件的記錄,而外聯(lián)結(jié)則保留其中部分。外左聯(lián)結(jié)與外右聯(lián)結(jié)的區(qū)別在于如果用A左聯(lián)結(jié)B則A中所有記錄都會保留在結(jié)果中,此時(shí)B中只有符合聯(lián)結(jié)條件的記錄,而右聯(lián)結(jié)相反,這樣也就不會混淆
了。
一.Join語法概述
join 用于多表中字段之間的聯(lián)系,語法如下:
... FROM table1 INNER|LEFT|RIGHT JOIN table2 ON conditiona
table1:左表;table2:右表。
JOIN 按照功能大致分為如下三類:
INNER JOIN(內(nèi)連接,或等值連接):取得兩個(gè)表中存在連接匹配關(guān)系的記錄。
LEFT JOIN(左連接):取得左表(table1)完全記錄,即是右表(table2)并無對應(yīng)匹配記錄。
RIGHT JOIN(右連接):與 LEFT JOIN 相反,取得右表(table2)完全記錄,即是左表(table1)并無匹配對應(yīng)記錄。
注意:mysql不支持Full join,不過可以通過UNION 關(guān)鍵字來合并 LEFT JOIN 與 RIGHT JOIN來模擬FULL join.
接下來給出一個(gè)列子用于解釋下面幾種分類。如下兩個(gè)表(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
內(nèi)連接,也叫等值連接,inner join產(chǎn)生同時(shí)符合A和B的一組數(shù)據(jù)。
mysql> select * from A inner join B on A.name = B.name; +----+--------+----+--------+ | id | name | id | name | +----+--------+----+--------+ | 1 | Pirate | 2 | Pirate | | 3 | Ninja | 4 | Ninja | +----+--------+----+--------+
三.Left join
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中兩者等價(jià),推薦使用left join.)左連接從左表(A)產(chǎn)生一套完整的記錄,與匹配的記錄(右表(B)) .如果沒有匹配,右側(cè)將包含null
如果想只從左表(A)中產(chǎn)生一套記錄,但不包含右表(B)的記錄,可以通過設(shè)置where語句來執(zhí)行,如下:
mysql> select * from A left join B on A.name=B.name where A.id is null or B.id is null; +----+-----------+------+------+ | 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)
求差集:
根據(jù)上面的例子可以求差集,如下:
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; # 結(jié)果 +------+-----------+------+-------------+ | 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
cross join:交叉連接,得到的結(jié)果是兩個(gè)表的乘積,即笛卡爾積
笛卡爾(Descartes)乘積又叫直積。假設(shè)集合A={a,b},集合B={0,1,2},則兩個(gè)集合的笛卡爾積為{(a,0),(a,1),(a,2),(b,0),(b,1), (b,2)}??梢詳U(kuò)展到多個(gè)集合的情況。類似的例子有,如果A表示某學(xué)校學(xué)生的集合,B表示該學(xué)校所有課程的集合,則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)
#再執(zhí)行:mysql> select * from A inner join B; 試一試
#在執(zhí)行mysql> select * from A cross join B on A.name = B.name; 試一試
實(shí)際上,在 MySQL 中(僅限于 MySQL) CROSS JOIN 與 INNER JOIN 的表現(xiàn)是一樣的,在不指定 ON 條件得到的結(jié)果都是笛卡爾積,反之取得兩個(gè)表完全匹配的結(jié)果。 INNER JOIN 與 CROSS JOIN 可以省略 INNER 或 CROSS 關(guān)鍵字,因此下面的 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)
全連接產(chǎn)生的所有記錄(雙方匹配記錄)在表A和表B。如果沒有匹配,則對面將包含null。
七.性能優(yōu)化
1.顯示(explicit) inner join VS 隱式(implicit) inner join
如:
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;
我在數(shù)據(jù)庫中比較(10w數(shù)據(jù))得之,它們用時(shí)幾乎相同,第一個(gè)是顯示的inner join,后一個(gè)是隱式的inner join。
2.left join/right join VS inner join
盡量用inner join.避免 LEFT JOIN 和 NULL.
在使用left join(或right join)時(shí),應(yīng)該清楚的知道以下幾點(diǎn):
(1). on與 where的執(zhí)行順序
ON 條件(“A LEFT JOIN B ON 條件表達(dá)式”中的ON)用來決定如何從 B 表中檢索數(shù)據(jù)行。如果 B 表中沒有任何一行數(shù)據(jù)匹配 ON 的條件,將會額外生成一行所有列為 NULL 的數(shù)據(jù),在匹配階段 WHERE 子句的條件都不會被使用。僅在匹配階段完成以后,WHERE 子句條件才會被使用。它將從匹配階段產(chǎn)生的數(shù)據(jù)中檢索過濾。
所以我們要注意:在使用Left (right) join的時(shí)候,一定要在先給出盡可能多的匹配滿足條件,減少Where的執(zhí)行。如:
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; 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的條件。從執(zhí)行性能來看第二個(gè)顯然更加省時(shí)。
(2).注意ON 子句和 WHERE 子句的不同
如作者舉了一個(gè)列子:
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表中檢索符合的所有數(shù)據(jù)行。第二條查詢做了簡單的LEFT JOIN,然后使用 WHERE 子句從 LEFT JOIN的數(shù)據(jù)中過濾掉不符合條件的數(shù)據(jù)行。
(3).盡量避免子查詢,而用join
往往性能這玩意兒,更多時(shí)候體現(xiàn)在數(shù)據(jù)量比較大的時(shí)候,此時(shí),我們應(yīng)該避免復(fù)雜的子查詢。如下:
insert into t1(a1) select b1 from t2 where not exists(select 1 from t1 where t1.id = t2.r_id);
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;
聲明:本網(wǎng)頁內(nèi)容旨在傳播知識,若有侵權(quán)等問題請及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com