創建兩張表並插入一些數據 查看表中的數據 比較下麵幾組查詢結果--如果你對left join足夠熟悉的話,先不要看結果,是否可以直接說出下麵查詢的結果 如果on後面的條件是左表中的列(and leftTable.colName='***'),左表中滿足條件的行和右表中的行進行匹配(根據on left ...
創建兩張表並插入一些數據
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 l