1. 程式人生 > >mysql/mariadb學習記錄——連接查詢(JOIN)

mysql/mariadb學習記錄——連接查詢(JOIN)

數據表格 ner 邊表 from join carte nbsp str --

//本文使用的數據表格
//persons表中id_p為主鍵
//orders表中id_o為主鍵,id_p為外鍵參考persons表中的id_p
mysql> select * from persons;select * from orders; +------+----------+-----------+----------------+----------+ | id_p | lastname | firstname | address | city | +------+----------+-----------+----------------+----------+ | 1
| Adams | John | Oxford Street | London | | 2 | Bush | George | Fifth Avenue | New York | | 3 | Carter | Thomas | Changan Street | Beijing | +------+----------+-----------+----------------+----------+ 3 rows in set (0.05 sec) +------+---------+------+ | id_o | orderNo | id_p | +------+---------+------+ | 1
| 77895 | 3 | | 2 | 44678 | 3 | | 3 | 22456 | 1 | | 4 | 24562 | 1 | | 5 | 34764 | 65 | +------+---------+------+ 5 rows in set (0.05 sec)

1.內連接 [inner] join:

  內連接也叫連接,或者自然連接:

//inner可刪
mysql> select lastname,firstname,orderNo from persons inner join orders on persons.id_p=orders.id_p order by lastname;
+----------+-----------+---------+ | lastname | firstname | orderNo | +----------+-----------+---------+ | Adams | John | 24562 | | Adams | John | 22456 | | Carter | Thomas | 77895 | | Carter | Thomas | 44678 | +----------+-----------+---------+ //使用where達到同樣的效果 mysql> select lastname,firstname,orderNo from persons,orders where persons.id_p=orders.id_p; +----------+-----------+---------+ | lastname | firstname | orderNo | +----------+-----------+---------+ | Carter | Thomas | 77895 | | Carter | Thomas | 44678 | | Adams | John | 22456 | | Adams | John | 24562 | +----------+-----------+---------+

2.左連接 left [outer] join:

  先看左連接的語句與查詢結果

//outer可刪去
mysql> select lastname,firstname,orderNo from persons left join orders on persons.id_p=orders.id_p order by lastname;
+----------+-----------+---------+
| lastname | firstname | orderNo |
+----------+-----------+---------+
| Adams    | John      | 24562   |
| Adams    | John      | 22456   |
| Bush     | George    | NULL    |
| Carter   | Thomas    | 77895   |
| Carter   | Thomas    | 44678   |
+----------+-----------+---------+

  通過這個結果我們可以發現lastname為Bush沒有orderNo,而lastname,firstname屬性是來自persons表,orderNo屬性是來自orders表可見左連接的作用時把在left join 左邊persons表格的所有信息都顯示出來,即使是沒有和left join右邊的表 orders中字段相匹配的信息也顯示出來,並且給orderNo的值填為NULL。

  如果把left join兩側的 persons和 orders位置互換那麽結果顯示的就是left join 左邊表orders表的所有信息,同時把未匹配到的left join右邊表persons中的lastname與firstname值置為NULL,這個結果與未交換join兩個表名位置的右連接的結果一致。

//將left join兩側的 persons和 orders位置互換
mysql> select lastname,firstname,orderNo from orders left join persons on persons.id_p=orders.id_p order by lastname;
+----------+-----------+---------+
| lastname | firstname | orderNo |
+----------+-----------+---------+
| NULL     | NULL      | 34764   |
| Adams    | John      | 24562   |
| Adams    | John      | 22456   |
| Carter   | Thomas    | 44678   |
| Carter   | Thomas    | 77895   |
+----------+-----------+---------+

3.右連接 right [outer] join:

//outer可刪去
mysql> select lastname,firstname,orderNo from persons right outer join orders on persons.id_p=orders.id_p order by lastname;
+----------+-----------+---------+
| lastname | firstname | orderNo |
+----------+-----------+---------+
| NULL     | NULL      | 34764   |
| Adams    | John      | 22456   |
| Adams    | John      | 24562   |
| Carter   | Thomas    | 77895   |
| Carter   | Thomas    | 44678   |
+----------+-----------+---------+

  同理與左連接,結果顯示的就是right join 左邊表persons表的所有信息,同時把未匹配到的right join右邊表orders中的orderNo值置為NULL。

4.全連接full join(不過mysql/mariadb不支持)

  全連接則是把兩個表的信息全部顯示出來,無論兩個表的信息匹配與否,未匹配上的信息都置為NULL。

mysql/mariadb學習記錄——連接查詢(JOIN)