首頁  >  文章  >  資料庫  >  mysql內連線和外連線有什麼差別

mysql內連線和外連線有什麼差別

青灯夜游
青灯夜游原創
2022-01-06 14:50:2617848瀏覽

mysql內連接和外連接的區別:內連接會取出連接表中匹配到的數據,匹配不到的不保留;而外連接會取出連接表中匹配到的數據,匹配不到的也會保留,其值為NULL。

mysql內連線和外連線有什麼差別

本教學操作環境: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影片教學

以上是mysql內連線和外連線有什麼差別的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn