首頁  >  文章  >  資料庫  >  Mysql Join的使用教學

Mysql Join的使用教學

零下一度
零下一度原創
2017-05-15 10:21:551447瀏覽

Mysql Join的使用

在前幾章節中,我們已經學會瞭如果在一張表中讀取數據,這是相對簡單的,但是在真正的應用中經常需要從多個數據表中讀取資料。

本章節我們將向大家介紹如何使用MySQL 的 JOIN 在兩個或多個表格中查詢資料。

你可以在SELECT, UPDATE 和 DELETE 語句中使用Mysql 的 join 來聯合多表查詢。

以下我們將示範MySQL LEFT JOIN 和 JOIN 的使用的差異。

在命令提示字元中使用JOIN

我們在TUTORIALS資料庫中有兩張表 tcount_tbl 和 tutorials_tbl。兩張資料表資料如下:

嘗試以下實例:

root@host# mysql -u root -p password;
Enter password:*******
mysql> use TUTORIALS;
Database changed
mysql> SELECT * FROM tcount_tbl;
+-----------------+----------------+
| tutorial_author | tutorial_count |
+-----------------+----------------+
| mahran          |             20 |
| mahnaz          |           NULL |
| Jen             |           NULL |
| Gill            |             20 |
| John Poul       |              1 |
| Sanjay          |              1 |
+-----------------+----------------+
6 rows in set (0.01 sec)
mysql> SELECT * from tutorials_tbl;
+-------------+----------------+-----------------+-----------------+
| tutorial_id | tutorial_title | tutorial_author | submission_date |
+-------------+----------------+-----------------+-----------------+
|           1 | Learn PHP      | John Poul       | 2007-05-24      |
|           2 | Learn MySQL    | Abdul S         | 2007-05-24      |
|           3 | JAVA Tutorial  | Sanjay          | 2007-05-06      |
+-------------+----------------+-----------------+-----------------+
3 rows in set (0.00 sec)
mysql>

接下來我們就使用MySQL的JOIN來連接以上兩張表來讀取tutorials_tbl表中所有tutorial_author欄位在tcount_tbl表對應的tutorial_count欄位值:

mysql> SELECT a.tutorial_id, a.tutorial_author, b.tutorial_count    -> FROM tutorials_tbl a, tcount_tbl b    -> WHERE a.tutorial_author = b.tutorial_author;+-------------+-----------------+----------------+| tutorial_id | tutorial_author | tutorial_count |+-------------+-----------------+----------------+|           1 | John Poul       |              1 ||           3 | Sanjay          |              1 |+-------------+-----------------+----------------+2 rows in set (0.01 sec)mysql>

在PHP腳本中使用JOIN

PHP 中使用mysql_query()函數來執行SQL語句,你可以使用以上的相同的SQL語句作為mysql_query()函數的參數。

嘗試如下實例:

<?php$dbhost = &#39;localhost:3036&#39;;$dbuser = &#39;root&#39;;$dbpass = &#39;rootpassword&#39;;$conn = mysql_connect($dbhost, $dbuser, $dbpass);if(! $conn ){  die(&#39;Could not connect: &#39; . mysql_error());}$sql = &#39;SELECT a.tutorial_id, a.tutorial_author, b.tutorial_count        FROM tutorials_tbl a, tcount_tbl b        WHERE a.tutorial_author = b.tutorial_author&#39;;mysql_select_db(&#39;TUTORIALS&#39;);$retval = mysql_query( $sql, $conn );if(! $retval ){  die(&#39;Could not get data: &#39; . mysql_error());}while($row = mysql_fetch_array($retval, MYSQL_ASSOC)){    echo "Author:{$row[&#39;tutorial_author&#39;]}  <br> ".         "Count: {$row[&#39;tutorial_count&#39;]} <br> ".         "Tutorial ID: {$row[&#39;tutorial_id&#39;]} <br> ".         "--------------------------------<br>";} echo "Fetched data successfully\n";mysql_close($conn);?>

MySQL LEFT JOIN

MySQL left join 與 join 有所不同。 MySQL LEFT JOIN 會讀取左邊數據表的全部數據,即便右邊表無對應數據。

實例

尝试以下实例,理解MySQL LEFT JOIN的应用:
root@host# mysql -u root -p password;Enter password:*******mysql> use TUTORIALS;Database changedmysql> SELECT a.tutorial_id, a.tutorial_author, b.tutorial_count    -> FROM tutorials_tbl a LEFT JOIN tcount_tbl b    -> ON a.tutorial_author = b.tutorial_author;+-------------+-----------------+----------------+| tutorial_id | tutorial_author | tutorial_count |+-------------+-----------------+----------------+|           1 | John Poul       |              1 ||           2 | Abdul S         |           NULL ||           3 | Sanjay          |              1 |+-------------+-----------------+----------------+3 rows in set (0.02 sec)

以上實例中使用了LEFT JOIN,該語句會讀取左邊的數據表tutorials_tbl的所有選取的字段數據,即使在右側表tcount_tbl中沒有對應的tutorial_author字段值。

【相關推薦】

1. 特別推薦「php程式設計師工具箱」V0.1版本下載

2. 免費mysql線上影片教學

#3. 資料庫設計那些事

#

以上是Mysql Join的使用教學的詳細內容。更多資訊請關注PHP中文網其他相關文章!

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