91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

sql中的left join及on、where條件關鍵字的區別詳解

發布時間:2020-08-20 06:39:00 來源:腳本之家 閱讀:228 作者:abce 欄目:數據庫

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關鍵字的區別詳解,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對億速云網站的支持!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

祁门县| 班玛县| 龙海市| 白沙| 电白县| 丹寨县| 禹州市| 堆龙德庆县| 宜兴市| 通山县| 香格里拉县| 宝鸡市| 双鸭山市| 大城县| 六枝特区| 定安县| 元氏县| 临颍县| 普安县| 湾仔区| 柏乡县| 石台县| 莱芜市| 万州区| 福泉市| 乡城县| 剑河县| 孝昌县| 邵东县| 泌阳县| 南雄市| 东台市| 大安市| 清徐县| 玛曲县| 江陵县| 射阳县| 大关县| 法库县| 汨罗市| 中西区|