在前幾章節中,我們已經學會了如果在一張表中讀取數據,這是相對簡單的,但是在真正的應用中經常需要從多個數據表中讀取數據。
本章節我們將向大家介紹如何使用 MySQL 的 JOIN 在兩個或多個表中查詢數據。
你可以在SELECT, UPDATE 和 DELETE 語句中使用 Mysql 的 JOIN 來聯合多表查詢。
JOIN 按照功能大致分為如下三類:
本章節使用的數據庫結構及數據下載:RUNOOB.sql。
我們在RUNOOB數據庫中有兩張表 tcount_tbl 和 runoob_tbl。兩張數據表數據如下:
嘗試以下實例:
root@host# mysql -u root -p password; Enter password:******* mysql> use RUNOOB; Database changed mysql> SELECT * FROM tcount_tbl; +-----------------+----------------+ | runoob_author | runoob_count | +-----------------+----------------+ | mahran | 20 | | mahnaz | NULL | | Jen | NULL | | Gill | 20 | | John Poul | 1 | | Sanjay | 1 | +-----------------+----------------+ 6 rows in set (0.01 sec) mysql> SELECT * from runoob_tbl; +-------------+----------------+-----------------+-----------------+ | runoob_id | runoob_title | runoob_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的INNER JOIN(也可以省略 INNER 使用 JOIN,效果一樣)來連接以上兩張表來讀取runoob_tbl表中所有runoob_author字段在tcount_tbl表對應的runoob_count字段值:
mysql> SELECT a.runoob_id, a.runoob_author, b.runoob_count FROM runoob_tbl a INNER JOIN tcount_tbl b ON a.runoob_author = b.runoob_author; +-----------+---------------+--------------+ | runoob_id | runoob_author | runoob_count | +-----------+---------------+--------------+ | 1 | John Poul | 1 | | 3 | Sanjay | 1 | +-----------+---------------+--------------+ 2 rows in set (0.00 sec)
以上 SQL 語句等價於:
mysql> SELECT a.runoob_id, a.runoob_author, b.runoob_count FROM runoob_tbl a, tcount_tbl b WHERE a.runoob_author = b.runoob_author; +-------------+-----------------+----------------+ | runoob_id | runoob_author | runoob_count | +-------------+-----------------+----------------+ | 1 | John Poul | 1 | | 3 | Sanjay | 1 | +-------------+-----------------+----------------+ 2 rows in set (0.01 sec) mysql>
MySQL left join 與 join 有所不同。 MySQL LEFT JOIN 會讀取左邊數據表的全部數據,即便右邊表無對應數據。
嘗試以下實例,以 runoob_tbl 為左表,tcount_tbl 為右表,理解MySQL LEFT JOIN的應用:
root@host# mysql -u root -p password; Enter password:******* mysql> use RUNOOB; Database changed mysql> SELECT a.runoob_id, a.runoob_author, b.runoob_count FROM runoob_tbl a LEFT JOIN tcount_tbl b ON a.runoob_author = b.runoob_author; +-------------+-----------------+----------------+ | runoob_id | runoob_author | runoob_count | +-------------+-----------------+----------------+ | 1 | John Poul | 1 | | 2 | Abdul S | NULL | | 3 | Sanjay | 1 | +-------------+-----------------+----------------+ 3 rows in set (0.02 sec)
以上實例中使用了LEFT JOIN,該語句會讀取左邊的數據表runoob_tbl的所有選取的字段數據,即便在右側表tcount_tbl中沒有對應的runoob_author字段值。
MySQL RIGHT JOIN 會讀取右邊數據表的全部數據,即便左邊邊表無對應數據。
嘗試以下實例,以 tcount_tbl 為左表,runoob_tbl 為右表,理解MySQL RIGHT JOIN的應用:
root@host# mysql -u root -p password; Enter password:******* mysql> use RUNOOB; Database changed mysql> SELECT b.runoob_id, b.runoob_author, a.runoob_count FROM tcount_tbl a RIGHT JOIN runoob_tbl b ON a.runoob_author = b.runoob_author; +-------------+-----------------+----------------+ | runoob_id | runoob_author | runoob_count | +-------------+-----------------+----------------+ | 1 | John Poul | 1 | | 2 | Abdul S | NULL | | 3 | Sanjay | 1 | +-------------+-----------------+----------------+ 3 rows in set (0.02 sec)
以上實例中使用了 RIGHT JOIN,該語句會讀取右邊的數據表 runoob_tbl 的所有選取的字段數據,即便在左側表tcount_tbl中沒有對應的runoob_author字段值。
PHP 中使用mysql_query()函數來執行SQL語句,你可以使用以上的相同的SQL語句作為mysql_query()函數的參數。
嘗試如下實例:
<?php $dbhost = 'localhost:3036'; $dbuser = 'root'; $dbpass = 'rootpassword'; $conn = mysql_connect($dbhost, $dbuser, $dbpass); if(! $conn ) { die('Could not connect: ' . mysql_error()); } $sql = 'SELECT a.runoob_id, a.runoob_author, b.runoob_count FROM runoob_tbl a INNER JOIN tcount_tbl b ON a.runoob_author = b.runoob_author'; mysql_select_db('RUNOOB'); $retval = mysql_query( $sql, $conn ); if(! $retval ) { die('Could not get data: ' . mysql_error()); } while($row = mysql_fetch_array($retval, MYSQL_ASSOC)) { echo "Author:{$row['runoob_author']} <br> ". "Count: {$row['runoob_count']} <br> ". "Tutorial ID: {$row['runoob_id']} <br> ". "--------------------------------<br>"; } echo "Fetched data successfully\n"; mysql_close($conn); ?>