Mysql 連接的使用
Mysql 連接的使用
在前幾章節(jié)中,我們已經(jīng)學(xué)會(huì)了如何在一張表中讀取數(shù)據(jù),這是相對(duì)簡(jiǎn)單的,但是在真正的應(yīng)用中經(jīng)常需要從多個(gè)數(shù)據(jù)表中讀取數(shù)據(jù)。
本章節(jié)我們將向大家介紹如何使用 MySQL 的 JOIN 在兩個(gè)或多個(gè)表中查詢(xún)數(shù)據(jù)。
你可以在 SELECT, UPDATE 和 DELETE 語(yǔ)句中使用 Mysql 的 JOIN 來(lái)聯(lián)合多表查詢(xún)。
JOIN 按照功能大致分為如下三類(lèi):
INNER JOIN(內(nèi)連接,或等值連接):獲取兩個(gè)表中字段匹配關(guān)系的記錄。
LEFT JOIN(左連接):獲取左表所有記錄,即使右表沒(méi)有對(duì)應(yīng)匹配的記錄。
RIGHT JOIN(右連接): 與 LEFT JOIN 相反,用于獲取右表所有記錄,即使左表沒(méi)有對(duì)應(yīng)匹配的記錄。
本章節(jié)使用的數(shù)據(jù)庫(kù)結(jié)構(gòu)及數(shù)據(jù)下載:php.sql。
在命令提示符中使用 INNER JOIN
我們?cè)赑HP數(shù)據(jù)庫(kù)中有兩張表 tcount_tbl 和php_tbl。兩張數(shù)據(jù)表數(shù)據(jù)如下:
實(shí)例
嘗試以下實(shí)例:
測(cè)試實(shí)例數(shù)據(jù)
mysql> use php; Database changed mysql> SELECT * FROM tcount_tbl; +---------------+--------------+ | php_author | php_count | +---------------+--------------+ | PHP中文網(wǎng) | 10 | | PHP.CN | 20 | | Google | 22 | +---------------+--------------+ 3 rows in set (0.01 sec) mysql> SELECT * from php_tbl; +-----------+---------------+---------------+-----------------+ | php_id | php_title | php_author | submission_date | +-----------+---------------+---------------+-----------------+ | 1 | 學(xué)習(xí) PHP | PHP中文網(wǎng) | 2017-04-12 | | 2 | 學(xué)習(xí) MySQL | PHP中文網(wǎng) | 2017-04-12 | | 3 | 學(xué)習(xí) Java | PHP.CN | 2015-05-01 | | 4 | 學(xué)習(xí) Python | PHP.CN | 2016-03-06 | | 5 | 學(xué)習(xí) C | PHP | 2017-04-05 | +-----------+---------------+---------------+-----------------+ 5 rows in set (0.01 sec)
接下來(lái)我們就使用MySQL的INNER JOIN(也可以省略 INNER 使用 JOIN,效果一樣)來(lái)連接以上兩張表來(lái)讀取php_tbl表中所有php_author字段在tcount_tbl表對(duì)應(yīng)的php_count字段值:
INNER JOIN
mysql> SELECT a.php_id, a.user_author, b.user_count FROM user_tbl a INNER JOIN tcount_tbl b ON a.user_author = b.user_author; +-------------+-----------------+----------------+ | a.user_id | a.user_author | b.user_count | +-------------+-----------------+----------------+ | 1 | PHP中文網(wǎng) | 10 | | 2 | PHP中文網(wǎng) | 10 | | 3 | PHP.CN | 20 | | 4 | PHP.CN | 20 | +-------------+-----------------+----------------+ 4 rows in set (0.00 sec)
以上 SQL 語(yǔ)句等價(jià)于:
WHERE 子句
mysql> SELECT a.user_id, a.user_author, b.user_count FROM user_tbl a, tcount_tbl b WHERE a.user_author = b.user_author; +-------------+-----------------+----------------+ | a.user_id | a.user_author | b.user_count | +-------------+-----------------+----------------+ | 1 | PHP中文網(wǎng) | 10 | | 2 | PHP中文網(wǎng) | 10 | | 3 |PHP.CN | 20 | | 4 | PHP.CN | 20 | +-------------+-----------------+----------------+ 4 rows in set (0.01 sec)
MySQL LEFT JOIN
MySQL left join 與 join 有所不同。 MySQL LEFT JOIN 會(huì)讀取左邊數(shù)據(jù)表的全部數(shù)據(jù),即便右邊表無(wú)對(duì)應(yīng)數(shù)據(jù)。
實(shí)例
嘗試以下實(shí)例,以 php_tbl 為左表,tcount_tbl 為右表,理解 MySQL LEFT JOIN 的應(yīng)用:
RIGHT JOIN mysql> SELECT a.user_id, a.user_author, b.user_count FROM user_tbl a RIGHT JOIN tcount_tbl b ON a.user_author = b.user_author; +-------------+-----------------+----------------+ | a.user_id | a.user_author | b.user_count | +-------------+-----------------+----------------+ | 1 | PHP中文網(wǎng) | 10 | | 2 | PHP中文網(wǎng) | 10 | | 3 | PHP.CN | 20 | | 4 | PHP.CN | 20 | | NULL | NULL | 22 | +-------------+-----------------+----------------+ 5 rows in set (0.01 sec)
以上實(shí)例中使用了 RIGHT JOIN,該語(yǔ)句會(huì)讀取右邊的數(shù)據(jù)表 tcount_tbl 的所有選取的字段數(shù)據(jù),即便在左側(cè)表php_tbl 中沒(méi)有對(duì)應(yīng)的php_author 字段值。
MySQL RIGHT JOIN
MySQL RIGHT JOIN 會(huì)讀取右邊數(shù)據(jù)表的全部數(shù)據(jù),即便左邊邊表無(wú)對(duì)應(yīng)數(shù)據(jù)。
實(shí)例
嘗試以下實(shí)例,以 php_tbl 為左表,tcount_tbl 為右表,理解MySQL RIGHT JOIN的應(yīng)用:
RIGHT JOIN mysql> SELECT a.user_id, a.user_author, b.user_count FROM user_tbl a RIGHT JOIN tcount_tbl b ON a.user_author = b.user_author; +-------------+-----------------+----------------+ | a.user_id | a.user_author | b.user_count | +-------------+-----------------+----------------+ | 1 | PHP中文網(wǎng) | 10 | | 2 | PHP中文網(wǎng) | 10 | | 3 |PHP.CN | 20 | | 4 | PHP.CN | 20 | | NULL | NULL | 22 | +-------------+-----------------+----------------+5 rows in set (0.01 sec)
以上實(shí)例中使用了 RIGHT JOIN,該語(yǔ)句會(huì)讀取右邊的數(shù)據(jù)表 tcount_tbl 的所有選取的字段數(shù)據(jù),即便在左側(cè)表 user_tbl 中沒(méi)有對(duì)應(yīng)的user_author 字段值。
在 PHP 腳本中使用 JOIN
PHP 中使用 mysqli_query() 函數(shù)來(lái)執(zhí)行 SQL 語(yǔ)句,你可以使用以上的相同的 SQL 語(yǔ)句作為 mysqli_query() 函數(shù)的參數(shù)。
嘗試如下實(shí)例:
header("Content-Type: text/html;charset=utf-8");
$dbhost = 'localhost'; // mysql服務(wù)器主機(jī)地址
$dbuser = 'root'; // mysql用戶(hù)名
$dbpass = 'root'; // mysql用戶(hù)名密碼
$conn = mysqli_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
die('連接失敗: ' . mysqli_error($conn));
}
// 設(shè)置編碼,防止中文亂碼
mysqli_query($conn , "set names utf8");
$sql = 'SELECT a.php_id, a.php_author, b.php_count FROM php_tbl a INNER JOIN tcount_tbl b ON a.php_author = b.php_author';
mysqli_select_db( $conn, 'php' );
$retval = mysqli_query( $conn, $sql );
if(! $retval )
{
die('無(wú)法讀取數(shù)據(jù): ' . mysqli_error($conn));
}
echo '<h2>PHP中文網(wǎng) MySQL JOIN 測(cè)試<h2>';
echo '<table border="1"><tr><td>教程 ID</td><td>作者</td><td>登陸次數(shù)</td></tr>';
while($row = mysqli_fetch_array($retval, MYSQL_ASSOC))
{
echo "<tr><td> {$row['php_id']}</td> ".
"<td>{$row['php_author']} </td> ".
"<td>{$row['php_count']} </td> ".
"</tr>";
}
echo '</table>';
mysqli_close($conn);
?>
效果圖:
相關(guān)視頻教程推薦:MySQL 連接的概念和類(lèi)型