LEFT JOIN 關(guān)鍵字會(huì)從左表 (table_name1) 那里返回所有的行,即使在右表 (table_name2) 中沒有匹配的行。
成都創(chuàng)新互聯(lián)公司專注于梨林企業(yè)網(wǎng)站建設(shè),成都響應(yīng)式網(wǎng)站建設(shè)公司,商城網(wǎng)站制作。梨林網(wǎng)站建設(shè)公司,為梨林等地區(qū)提供建站服務(wù)。全流程按需定制,專業(yè)設(shè)計(jì),全程項(xiàng)目跟蹤,成都創(chuàng)新互聯(lián)公司專業(yè)和態(tài)度為您提供的服務(wù)
LEFT JOIN 關(guān)鍵字語法
SELECT column_name(s) FROM table_name1 LEFT JOIN table_name2 ON table_name1.column_name=table_name2.column_name
注釋:在某些數(shù)據(jù)庫(kù)中, LEFT JOIN 稱為 LEFT OUTER JOIN。
創(chuàng)建兩張表并插入一些數(shù)據(jù)
create table class( class_id int, class_name varchar(20), class_grade char(1) ); insert into class values (1,'語文','A'); insert into class values (2,'數(shù)學(xué)','B'); insert into class values (3,'英語','C'); create table score( class_id int, stu_id varchar(20), Score int ); insert into score values (1,'A001',91); insert into score values (2,'A001',95); insert into score values (1,'A002',82); insert into score values (2,'A002',87); insert into score values (3,'B003',65);
查看表中的數(shù)據(jù)
MySQL> select * from class; +----------+------------+-------------+ | class_id | class_name | class_grade | +----------+------------+-------------+ | 1 | 語文 | A | | 2 | 數(shù)學(xué) | B | | 3 | 英語 | C | +----------+------------+-------------+ 3 rows in set (0.00 sec) mysql> select * from score; +----------+--------+-------+ | class_id | stu_id | Score | +----------+--------+-------+ | 1 | A001 | 91 | | 2 | A001 | 95 | | 1 | A002 | 82 | | 2 | A002 | 87 | | 3 | B003 | 65 | +----------+--------+-------+ 5 rows in set (0.00 sec) mysql>
比較下面幾組查詢結(jié)果
--如果你對(duì)left join足夠熟悉的話,先不要看結(jié)果,是否可以直接說出下面查詢的結(jié)果
mysql> select * from class A left join score B on A.class_id=B.class_id; +----------+------------+-------------+----------+--------+-------+ | class_id | class_name | class_grade | class_id | stu_id | Score | +----------+------------+-------------+----------+--------+-------+ | 1 | 語文 | A | 1 | A001 | 91 | | 2 | 數(shù)學(xué) | B | 2 | A001 | 95 | | 1 | 語文 | A | 1 | A002 | 82 | | 2 | 數(shù)學(xué) | B | 2 | A002 | 87 | | 3 | 英語 | C | 3 | B003 | 65 | +----------+------------+-------------+----------+--------+-------+ 5 rows in set (0.00 sec) mysql> select * from class A left join score B on A.class_id=B.class_id and 1=1; +----------+------------+-------------+----------+--------+-------+ | class_id | class_name | class_grade | class_id | stu_id | Score | +----------+------------+-------------+----------+--------+-------+ | 1 | 語文 | A | 1 | A001 | 91 | | 2 | 數(shù)學(xué) | B | 2 | A001 | 95 | | 1 | 語文 | A | 1 | A002 | 82 | | 2 | 數(shù)學(xué) | B | 2 | A002 | 87 | | 3 | 英語 | C | 3 | B003 | 65 | +----------+------------+-------------+----------+--------+-------+ 5 rows in set (0.01 sec) mysql> select * from class A left join score B on A.class_id=B.class_id and 1=0; +----------+------------+-------------+----------+--------+-------+ | class_id | class_name | class_grade | class_id | stu_id | Score | +----------+------------+-------------+----------+--------+-------+ | 1 | 語文 | A | NULL | NULL | NULL | | 2 | 數(shù)學(xué) | B | NULL | NULL | NULL | | 3 | 英語 | C | NULL | NULL | NULL | +----------+------------+-------------+----------+--------+-------+ 3 rows in set (0.00 sec) mysql> select * from class A left join score B on 1=0; +----------+------------+-------------+----------+--------+-------+ | class_id | class_name | class_grade | class_id | stu_id | Score | +----------+------------+-------------+----------+--------+-------+ | 1 | 語文 | A | NULL | NULL | NULL | | 2 | 數(shù)學(xué) | B | NULL | NULL | NULL | | 3 | 英語 | C | NULL | NULL | NULL | +----------+------------+-------------+----------+--------+-------+ 3 rows in set (0.00 sec)
mysql> left join的最重要特點(diǎn)是:不管on后面是什么條件,都會(huì)返回左表中的所有行!
mysql> select * from class A left join score B on A.class_id=B.class_id and A.class_name='語文'; +----------+------------+-------------+----------+--------+-------+ | class_id | class_name | class_grade | class_id | stu_id | Score | +----------+------------+-------------+----------+--------+-------+ | 1 | 語文 | A | 1 | A001 | 91 | | 1 | 語文 | A | 1 | A002 | 82 | | 2 | 數(shù)學(xué) | B | NULL | NULL | NULL | | 3 | 英語 | C | NULL | NULL | NULL | +----------+------------+-------------+----------+--------+-------+ 4 rows in set (0.00 sec) mysql> select * from class A left join score B on A.class_id=B.class_id and A.class_name='數(shù)學(xué)'; +----------+------------+-------------+----------+--------+-------+ | class_id | class_name | class_grade | class_id | stu_id | Score | +----------+------------+-------------+----------+--------+-------+ | 2 | 數(shù)學(xué) | B | 2 | A001 | 95 | | 2 | 數(shù)學(xué) | B | 2 | A002 | 87 | | 1 | 語文 | A | NULL | NULL | NULL | | 3 | 英語 | C | NULL | NULL | NULL | +----------+------------+-------------+----------+--------+-------+ 4 rows in set (0.00 sec) mysql> select * from class A left join score B on A.class_id=B.class_id and A.class_name='英語'; +----------+------------+-------------+----------+--------+-------+ | class_id | class_name | class_grade | class_id | stu_id | Score | +----------+------------+-------------+----------+--------+-------+ | 3 | 英語 | C | 3 | B003 | 65 | | 1 | 語文 | A | NULL | NULL | NULL | | 2 | 數(shù)學(xué) | B | NULL | NULL | NULL | +----------+------------+-------------+----------+--------+-------+ 3 rows in set (0.01 sec) mysql> select * from class A left join score B on A.class_id=B.class_id and A.class_name='體育'; +----------+------------+-------------+----------+--------+-------+ | class_id | class_name | class_grade | class_id | stu_id | Score | +----------+------------+-------------+----------+--------+-------+ | 1 | 語文 | A | NULL | NULL | NULL | | 2 | 數(shù)學(xué) | B | NULL | NULL | NULL | | 3 | 英語 | C | NULL | NULL | NULL | +----------+------------+-------------+----------+--------+-------+ 3 rows in set (0.00 sec) mysql>
如果on后面的條件是左表中的列(and leftTable.colName='***'),左表中滿足條件的行和右表中的行進(jìn)行匹配(根據(jù)on leftTable.id=rightTable.id);左表中不滿足條件的行,直接輸出,其對(duì)應(yīng)的右表中的列都是null。
mysql> select * from class A left join score B on A.class_id=B.class_id and B.Score=90; +----------+------------+-------------+----------+--------+-------+ | class_id | class_name | class_grade | class_id | stu_id | Score | +----------+------------+-------------+----------+--------+-------+ | 1 | 語文 | A | NULL | NULL | NULL | | 2 | 數(shù)學(xué) | B | NULL | NULL | NULL | | 3 | 英語 | C | NULL | NULL | NULL | +----------+------------+-------------+----------+--------+-------+ 3 rows in set (0.01 sec) mysql> select * from class A left join score B on A.class_id=B.class_id and B.Score=65; +----------+------------+-------------+----------+--------+-------+ | class_id | class_name | class_grade | class_id | stu_id | Score | +----------+------------+-------------+----------+--------+-------+ | 3 | 英語 | C | 3 | B003 | 65 | | 1 | 語文 | A | NULL | NULL | NULL | | 2 | 數(shù)學(xué) | B | NULL | NULL | NULL | +----------+------------+-------------+----------+--------+-------+ 3 rows in set (0.01 sec) mysql>
如果on后面的條件是右表中的列(and rightTable.colName='***'),首先會(huì)根據(jù)(and rightTable.colName='***')過濾掉右表中不滿足條件的行;然后,左表中的行根據(jù)(on leftTable.id=rightTable.id)和右表中滿足條件的行進(jìn)行匹配。
mysql> select * from class A left join score B on A.class_id=B.class_id and A.class_name='語文' and B.Score=90; +----------+------------+-------------+----------+--------+-------+ | class_id | class_name | class_grade | class_id | stu_id | Score | +----------+------------+-------------+----------+--------+-------+ | 1 | 語文 | A | NULL | NULL | NULL | | 2 | 數(shù)學(xué) | B | NULL | NULL | NULL | | 3 | 英語 | C | NULL | NULL | NULL | +----------+------------+-------------+----------+--------+-------+ 3 rows in set (0.00 sec) mysql> select * from class A left join score B on A.class_id=B.class_id and A.class_name='語文' and B.Score=91; +----------+------------+-------------+----------+--------+-------+ | class_id | class_name | class_grade | class_id | stu_id | Score | +----------+------------+-------------+----------+--------+-------+ | 1 | 語文 | A | 1 | A001 | 91 | | 2 | 數(shù)學(xué) | B | NULL | NULL | NULL | | 3 | 英語 | C | NULL | NULL | NULL | +----------+------------+-------------+----------+--------+-------+ 3 rows in set (0.01 sec) mysql> select * from class A left join score B on A.class_id=B.class_id and A.class_name='體育' and B.Score=90; +----------+------------+-------------+----------+--------+-------+ | class_id | class_name | class_grade | class_id | stu_id | Score | +----------+------------+-------------+----------+--------+-------+ | 1 | 語文 | A | NULL | NULL | NULL | | 2 | 數(shù)學(xué) | B | NULL | NULL | NULL | | 3 | 英語 | C | NULL | NULL | NULL | +----------+------------+-------------+----------+--------+-------+ 3 rows in set (0.00 sec) mysql> select * from class A left join score B on A.class_id=B.class_id and A.class_name='體育' and B.Score=82; +----------+------------+-------------+----------+--------+-------+ | class_id | class_name | class_grade | class_id | stu_id | Score | +----------+------------+-------------+----------+--------+-------+ | 1 | 語文 | A | NULL | NULL | NULL | | 2 | 數(shù)學(xué) | B | NULL | NULL | NULL | | 3 | 英語 | C | NULL | NULL | NULL | +----------+------------+-------------+----------+--------+-------+ 3 rows in set (0.00 sec) mysql>
/**********************過濾條件在on中時(shí)**********************/
總結(jié)一下,如果 left join on leftTable.id=rightTable.id 后還有其他條件:
(1)and leftTable.colName='***',過濾左表,但是左表不滿足條件的行直接輸出,并將右表對(duì)應(yīng)部分置為null
(2)and rightTable.colName='***',過濾右表,對(duì)左表沒有影響
(3)and leftTable.colName='***' and rightTable.colName='***',就是上面(1)和(2)一起發(fā)揮作用
不管on后面有哪些條件,left join都要返回左表中的所有行!
mysql> select * from class A left join score B on A.class_id=B.class_id where A.class_name='語文'; +----------+------------+-------------+----------+--------+-------+ | class_id | class_name | class_grade | class_id | stu_id | Score | +----------+------------+-------------+----------+--------+-------+ | 1 | 語文 | A | 1 | A001 | 91 | | 1 | 語文 | A | 1 | A002 | 82 | +----------+------------+-------------+----------+--------+-------+ 2 rows in set (0.01 sec) mysql> select * from class A left join score B on A.class_id=B.class_id where A.class_name='數(shù)學(xué)'; +----------+------------+-------------+----------+--------+-------+ | class_id | class_name | class_grade | class_id | stu_id | Score | +----------+------------+-------------+----------+--------+-------+ | 2 | 數(shù)學(xué) | B | 2 | A001 | 95 | | 2 | 數(shù)學(xué) | B | 2 | A002 | 87 | +----------+------------+-------------+----------+--------+-------+ 2 rows in set (0.00 sec) mysql> select * from class A left join score B on A.class_id=B.class_id where A.class_name='英語'; +----------+------------+-------------+----------+--------+-------+ | class_id | class_name | class_grade | class_id | stu_id | Score | +----------+------------+-------------+----------+--------+-------+ | 3 | 英語 | C | 3 | B003 | 65 | +----------+------------+-------------+----------+--------+-------+ 1 row in set (0.00 sec) mysql> select * from class A left join score B on A.class_id=B.class_id where A.class_name='體育'; Empty set (0.00 sec) mysql> select * from class A left join score B on A.class_id=B.class_id where B.Score=90; Empty set (0.01 sec) mysql> select * from class A left join score B on A.class_id=B.class_id where B.Score=91; +----------+------------+-------------+----------+--------+-------+ | class_id | class_name | class_grade | class_id | stu_id | Score | +----------+------------+-------------+----------+--------+-------+ | 1 | 語文 | A | 1 | A001 | 91 | +----------+------------+-------------+----------+--------+-------+ 1 row in set (0.00 sec) mysql> select * from class A left join score B on A.class_id=B.class_id where A.class_name='語文' and B.Score=90; Empty set (0.00 sec) mysql> select * from class A left join score B on A.class_id=B.class_id where A.class_name='語文' and B.Score=91; +----------+------------+-------------+----------+--------+-------+ | class_id | class_name | class_grade | class_id | stu_id | Score | +----------+------------+-------------+----------+--------+-------+ | 1 | 語文 | A | 1 | A001 | 91 | +----------+------------+-------------+----------+--------+-------+ 1 row in set (0.00 sec) mysql> select * from class A left join score B on A.class_id=B.class_id where A.class_name='體育' and B.Score=90; Empty set (0.00 sec) mysql> select * from class A left join score B on A.class_id=B.class_id where A.class_name='體育' and B.Score=91; Empty set (0.00 sec) mysql>
/**********************過濾條件在where中時(shí)**********************/
過濾條件寫在where中時(shí),先根據(jù)where條件對(duì)表進(jìn)行過濾,然后再執(zhí)行l(wèi)eft join
總結(jié)
以上所述是小編給大家介紹的sql中的left join及on、where關(guān)鍵字的區(qū)別詳解,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)創(chuàng)新互聯(lián)網(wǎng)站的支持!