迄今為止,我們每次只能從一張表里獲取數(shù)據(jù)。這足以應(yīng)付簡(jiǎn)單的任務(wù)了,但大多數(shù)真實(shí)的 MySQL 應(yīng)用場(chǎng)景卻經(jīng)常會(huì)需要通過(guò)一次查詢,從多張表中獲取數(shù)據(jù)。
在一個(gè) SQL 查詢中使用多張表,聯(lián)結(jié)(join)行為在 MySQL 數(shù)據(jù)庫(kù)中指的就是將2張或更多的表合為一張表。
你可以在 SELECT、UPDATE、DELETE語(yǔ)句中使用 JOIN 來(lái)聯(lián)結(jié) MySQL 表。下面還將介紹一個(gè)左聯(lián)結(jié)(LEFT JOIN)的范例,了解一下它與 JOIN 的區(qū)別。
假設(shè)數(shù)據(jù)庫(kù) 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>
上例通過(guò)一個(gè) SQL 查詢將兩張表聯(lián)結(jié)到一起。這次查詢選擇了表 tutorials_tbl 中所有的作者,然后獲取表 tcount_tbl 中這些作者相應(yīng)的教程數(shù)量。
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 腳本中使用以前學(xué)到過(guò)的任何一種 SQL 查詢。只需要將 SQL 查詢傳入 PHP 函數(shù) mysql_query() 中,就能按照之前的方式獲得結(jié)果。
相關(guān)范例如下:
<?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.tutorial_id, a.tutorial_author, b.tutorial_count
FROM tutorials_tbl a, tcount_tbl b
WHERE a.tutorial_author = b.tutorial_author';
mysql_select_db('TUTORIALS');
$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['tutorial_author']} <br> ".
"Count: {$row['tutorial_count']} <br> ".
"Tutorial ID: {$row['tutorial_id']} <br> ".
"--------------------------------<br>";
}
echo "Fetched data successfully\n";
mysql_close($conn);
?>
MySQL 的左聯(lián)結(jié)(LEFT JOIN)與簡(jiǎn)單使用 JOIN 的效果不同。左聯(lián)結(jié)側(cè)重考慮左側(cè)的表。
如果進(jìn)行左聯(lián)結(jié),除了得到所有跟以上聯(lián)結(jié)同樣的匹配記錄之外,還會(huì)得到左側(cè)表中未曾匹配的記錄,從而保證了(在該范例中)照顧到了每一位作者。
下面這個(gè)范例可以幫我們更好地理解左聯(lián)結(jié)。
root@host# mysql -u root -p password;
Enter password:*******
mysql> use TUTORIALS;
Database changed
mysql> 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)
必須多加練習(xí),才能熟悉 JOIN。這是 MySQL/SQL 中的一個(gè)比較復(fù)雜的概念,必須經(jīng)過(guò)一番真實(shí)的案例磨煉才能真正地掌握它。