到目前為止,我們只是從一個表讀取數據。這是相對簡單的,但在大多數現實中的MySQL使用,需要從多個表中,在單個查詢獲得數據。
可以在單個SQL查詢中使用多個表。連接MySQL中的行在兩個或多個表到一個表。
可以使用Join在SELECT,UPDATE和DELETE語句加入MySQL表。我們將看到LEFT JOIN的例子, 這與簡單的MySQL JOIN有所不同。
假設我們有兩個表 tcount_tbl 和 tutorials_tbl,在數據庫:test ,完整列表如下:
試試下面的例子:
root@host# mysql -u root -p password; Enter password: mysql> use test; 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>
現在,我們可以寫一個SQL查詢來連接這兩個表。此查詢將從表 tutorials_tbl 和 tcount_tbl 選擇所有作者的教程數量。
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>
可以使用上述的任何SQL查詢在PHP腳本中。只需要傳遞SQL查詢到PHP的mysql_query()函數,然後以通常的方式獲取結果。
試試下面的例子:
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$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('test');
$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左連接不同於簡單連接。MySQL LEFT JOIN提供該表額外字段在左側。
如果使用LEFT JOIN,得到的所有記錄的匹配方式相同,在左邊表中得到的每個記錄不匹配也會有一個額外的記錄。 從而確保(在本例子),每次作者信息都會列出:
試試下面的例子就明白了 LEFT JOIN 的用法(注:第2條,作者Abdul S沒有寫教程也被列出來了):
root@host# mysql -u root -p password;
Enter password:
mysql> use test;
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)
需要做更多的練習,以熟悉連接。這個概念是有點復雜,做這樣實際例子,在MySQL/SQL將變得更加清晰。