共計 3603 個字符,預計需要花費 10 分鐘才能閱讀完成。
這篇文章主要介紹“mysql 內連接和外連接有哪些區別”,在日常操作中,相信很多人在 mysql 內連接和外連接有哪些區別問題上存在疑惑,丸趣 TV 小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”mysql 內連接和外連接有哪些區別”的疑惑有所幫助!接下來,請跟著丸趣 TV 小編一起來學習吧!
mysql 內連接和外連接的區別:內連接會取出連接表中匹配到的數據,匹配不到的不保留;而外連接會取出連接表中匹配到的數據,匹配不到的也會保留,其值為 NULL。
本教程操作環境:windows7 系統、mysql8 版本、Dell G3 電腦。
區別
內連接(inner join):取出連接表中匹配到的數據,匹配不到的不保留
外連接(outer join):取出連接表中匹配到的數據,匹配不到的也會保留,其值為 NULL
示例表
users 表
mysql select * from users;
+----+-------+
| id | name |
+----+-------+
| 1 | john |
| 2 | May |
| 3 | Lucy |
| 4 | Jack |
| 5 | James |
+----+-------+
5 rows in set (0.00 sec)
topics 表
mysql select * from topics;
+----+---------------------------------------+---------+
| id | title | user_id |
+----+---------------------------------------+---------+
| 1 | Hello world | 1 |
| 2 | PHP is the best language in the world | 2 |
| 3 | Laravel artist | 6 |
+----+---------------------------------------+---------+
3 rows in set (0.00 sec)
內連接 (inner join)
示例
mysql select * from users as u inner join topics as t on u.id=t.user_id;
+----+------+----+---------------------------------------+---------+
| id | name | id | title | user_id |
+----+------+----+---------------------------------------+---------+
| 1 | john | 1 | Hello world | 1 |
| 2 | May | 2 | PHP is the best language in the world | 2 |
+----+------+----+---------------------------------------+---------+
2 rows in set (0.00 sec)
inner 可以省略,as 是給表起別名,也可以省略
mysql select * from users u join topics t on u.id=t.user_id;
+----+------+----+---------------------------------------+---------+
| id | name | id | title | user_id |
+----+------+----+---------------------------------------+---------+
| 1 | john | 1 | Hello world | 1 |
| 2 | May | 2 | PHP is the best language in the world | 2 |
+----+------+----+---------------------------------------+---------+
2 rows in set (0.00 sec)
以上兩句等價于
mysql select * from users,topics where users.id=topics.user_id;
+----+------+----+---------------------------------------+---------+
| id | name | id | title | user_id |
+----+------+----+---------------------------------------+---------+
| 1 | john | 1 | Hello world | 1 |
| 2 | May | 2 | PHP is the best language in the world | 2 |
+----+------+----+---------------------------------------+---------+
2 rows in set (0.00 sec)
外連接 (outer join)
左外連接(left outer join):以左邊的表為主表
右外連接(right outer join):以右邊的表為主表
以某一個表為主表,進行關聯查詢,不管能不能關聯的上,主表的數據都會保留,關聯不上的以 NULL 顯示
通俗解釋就是:先拿出主表的所有數據,然后到關聯的那張表去找有沒有符合關聯條件的數據,如果有,正常顯示,如果沒有,顯示為 NULL
示例
mysql select * from users as u left join topics as t on u.id=t.user_id;
+----+-------+------+---------------------------------------+---------+
| id | name | id | title | user_id |
+----+-------+------+---------------------------------------+---------+
| 1 | john | 1 | Hello world | 1 |
| 2 | May | 2 | PHP is the best language in the world | 2 |
| 3 | Lucy | NULL | NULL | NULL |
| 4 | Jack | NULL | NULL | NULL |
| 5 | James | NULL | NULL | NULL |
+----+-------+------+---------------------------------------+---------+
5 rows in set (0.00 sec)
等價于以下,只是字段的位置不一樣
mysql select * from topics as t right join users as u on u.id=t.user_id;
+------+---------------------------------------+---------+----+-------+
| id | title | user_id | id | name |
+------+---------------------------------------+---------+----+-------+
| 1 | Hello world | 1 | 1 | john |
| 2 | PHP is the best language in the world | 2 | 2 | May |
| NULL | NULL | NULL | 3 | Lucy |
| NULL | NULL | NULL | 4 | Jack |
| NULL | NULL | NULL | 5 | James |
+------+---------------------------------------+---------+----+-------+
5 rows in set (0.00 sec)
左外連接和右外連接是相對的,主要就是以哪個表為主表去進行關聯
到此,關于“mysql 內連接和外連接有哪些區別”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續學習更多相關知識,請繼續關注丸趣 TV 網站,丸趣 TV 小編會繼續努力為大家帶來更多實用的文章!