您好,登錄后才能下訂單哦!
LEFT JOIN 關鍵字會從左表 (table_name1) 那里返回所有的行,即使在右表 (table_name2) 中沒有匹配的行。
LEFT JOIN 關鍵字語法
SELECT column_name(s) FROM table_name1 LEFT JOIN table_name2 ON table_name1.column_name=table_name2.column_name
注釋:在某些數據庫中, LEFT JOIN 稱為 LEFT OUTER JOIN。
創建兩張表并插入一些數據
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,'數學','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);
查看表中的數據
mysql> select * from class; +----------+------------+-------------+ | class_id | class_name | class_grade | +----------+------------+-------------+ | 1 | 語文 | A | | 2 | 數學 | 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>
比較下面幾組查詢結果
--如果你對left join足夠熟悉的話,先不要看結果,是否可以直接說出下面查詢的結果
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 | 數學 | B | 2 | A001 | 95 | | 1 | 語文 | A | 1 | A002 | 82 | | 2 | 數學 | 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 | 數學 | B | 2 | A001 | 95 | | 1 | 語文 | A | 1 | A002 | 82 | | 2 | 數學 | 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 | 數學 | 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 | 數學 | B | NULL | NULL | NULL | | 3 | 英語 | C | NULL | NULL | NULL | +----------+------------+-------------+----------+--------+-------+ 3 rows in set (0.00 sec)
mysql> left join的最重要特點是:不管on后面是什么條件,都會返回左表中的所有行!
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 | 數學 | 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='數學'; +----------+------------+-------------+----------+--------+-------+ | class_id | class_name | class_grade | class_id | stu_id | Score | +----------+------------+-------------+----------+--------+-------+ | 2 | 數學 | B | 2 | A001 | 95 | | 2 | 數學 | 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 | 數學 | 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 | 數學 | B | NULL | NULL | NULL | | 3 | 英語 | C | NULL | NULL | NULL | +----------+------------+-------------+----------+--------+-------+ 3 rows in set (0.00 sec) mysql>
如果on后面的條件是左表中的列(and leftTable.colName='***'),左表中滿足條件的行和右表中的行進行匹配(根據on leftTable.id=rightTable.id);左表中不滿足條件的行,直接輸出,其對應的右表中的列都是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 | 數學 | 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 | 數學 | B | NULL | NULL | NULL | +----------+------------+-------------+----------+--------+-------+ 3 rows in set (0.01 sec) mysql>
如果on后面的條件是右表中的列(and rightTable.colName='***'),首先會根據(and rightTable.colName='***')過濾掉右表中不滿足條件的行;然后,左表中的行根據(on leftTable.id=rightTable.id)和右表中滿足條件的行進行匹配。
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 | 數學 | 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 | 數學 | 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 | 數學 | 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 | 數學 | B | NULL | NULL | NULL | | 3 | 英語 | C | NULL | NULL | NULL | +----------+------------+-------------+----------+--------+-------+ 3 rows in set (0.00 sec) mysql>
/**********************過濾條件在on中時**********************/
總結一下,如果 left join on leftTable.id=rightTable.id 后還有其他條件:
(1)and leftTable.colName='***',過濾左表,但是左表不滿足條件的行直接輸出,并將右表對應部分置為null
(2)and rightTable.colName='***',過濾右表,對左表沒有影響
(3)and leftTable.colName='***' and rightTable.colName='***',就是上面(1)和(2)一起發揮作用
不管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='數學'; +----------+------------+-------------+----------+--------+-------+ | class_id | class_name | class_grade | class_id | stu_id | Score | +----------+------------+-------------+----------+--------+-------+ | 2 | 數學 | B | 2 | A001 | 95 | | 2 | 數學 | 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中時**********************/
過濾條件寫在where中時,先根據where條件對表進行過濾,然后再執行left join
總結
以上所述是小編給大家介紹的sql中的left join及on、where關鍵字的區別詳解,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對億速云網站的支持!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。