到目前,我們已經學習了從一個表中擷取資料。這是簡單的需要,但在大多數現實MySQL的使用,經常需要將資料從多個表中的一個單一的查詢。
可以使用多個表中的單一SQL查詢。在MySQL中聯結(join)行為是指兩個或多個表到一個表中可以使用串連在SELECT,UPDATE和DELETE語句中加入MySQL表。我們將看到一個例子LEFT JOIN簡單的MySQL串連。
在命令提示字元使用聯結:
假設我們兩個表的教程tcount_tbl和tutorials_tbl的完整列表如下:
例子:
試試下面的例子:
root@host# mysql -u root -p password;Enter password:*******mysql> use TUTORIALS;Database changedmysql> 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>
在PHP指令碼中使用聯結:
可以使用任何上述的SQL查詢的PHP指令碼。只需要通過PHP函數mysql_query()執行SQL查詢,然後用常規方法擷取結果。
例子:
試試下面的例子:
<?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左串連:
一個簡單的串連和一個MySQL左串連是不同的。一個MySQL LEFT JOIN提供了額外的考慮到在左邊的表。
如果做了LEFT JOIN,得到的所有記錄以同樣的方式相匹配,此外,得到一個額外的記錄每個不匹配的記錄,在左表中的聯結 - 從而保證了每一個作者得到關聯(本例子中):
執行個體:
試試下面的例子就明白了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)
需要做更多的實踐才能熟悉JOINS。這是一個複雜的概念,在MySQL/SQL將變得更加清晰。