国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Mysql 連接的使用

Mysql 連接的使用

在前幾章節(jié)中,我們已經(jīng)學(xué)會了如何在一張表中讀取數(shù)據(jù),這是相對簡單的,但是在真正的應(yīng)用中經(jīng)常需要從多個數(shù)據(jù)表中讀取數(shù)據(jù)。

本章節(jié)我們將向大家介紹如何使用 MySQL 的 JOIN 在兩個或多個表中查詢數(shù)據(jù)。

你可以在 SELECT, UPDATE 和 DELETE 語句中使用 Mysql 的 JOIN 來聯(lián)合多表查詢。

JOIN 按照功能大致分為如下三類:

  • INNER JOIN(內(nèi)連接,或等值連接):獲取兩個表中字段匹配關(guān)系的記錄。

  • LEFT JOIN(左連接):獲取左表所有記錄,即使右表沒有對應(yīng)匹配的記錄。

  • RIGHT JOIN(右連接): 與 LEFT JOIN 相反,用于獲取右表所有記錄,即使左表沒有對應(yīng)匹配的記錄。

本章節(jié)使用的數(shù)據(jù)庫結(jié)構(gòu)及數(shù)據(jù)下載:php.sql。

在命令提示符中使用 INNER JOIN

我們在PHP數(shù)據(jù)庫中有兩張表 tcount_tbl 和php_tbl。兩張數(shù)據(jù)表數(shù)據(jù)如下:

實例

嘗試以下實例:

測試實例數(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)

接下來我們就使用MySQL的INNER JOIN(也可以省略 INNER 使用 JOIN,效果一樣)來連接以上兩張表來讀取php_tbl表中所有php_author字段在tcount_tbl表對應(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 語句等價于:

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)

img_innerjoin.gif

MySQL LEFT JOIN

MySQL left join 與 join 有所不同。 MySQL LEFT JOIN 會讀取左邊數(shù)據(jù)表的全部數(shù)據(jù),即便右邊表無對應(yīng)數(shù)據(jù)。

實例

嘗試以下實例,以 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)

以上實例中使用了 RIGHT JOIN,該語句會讀取右邊的數(shù)據(jù)表 tcount_tbl 的所有選取的字段數(shù)據(jù),即便在左側(cè)表php_tbl 中沒有對應(yīng)的php_author 字段值。

img_leftjoin.gif

MySQL RIGHT JOIN

MySQL RIGHT JOIN 會讀取右邊數(shù)據(jù)表的全部數(shù)據(jù),即便左邊邊表無對應(yīng)數(shù)據(jù)。

實例

嘗試以下實例,以 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)

以上實例中使用了 RIGHT JOIN,該語句會讀取右邊的數(shù)據(jù)表 tcount_tbl 的所有選取的字段數(shù)據(jù),即便在左側(cè)表 user_tbl 中沒有對應(yīng)的user_author 字段值。

img_rightjoin.gif

在 PHP 腳本中使用 JOIN

PHP 中使用 mysqli_query() 函數(shù)來執(zhí)行 SQL 語句,你可以使用以上的相同的 SQL 語句作為 mysqli_query() 函數(shù)的參數(shù)。

嘗試如下實例:

<?php
header("Content-Type: text/html;charset=utf-8");

$dbhost = 'localhost';  // mysql服務(wù)器主機地址
$dbuser = 'root';            // mysql用戶名
$dbpass = 'root';          // mysql用戶名密碼       
$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('無法讀取數(shù)據(jù): ' . mysqli_error($conn));
}
echo '<h2>PHP中文網(wǎng) MySQL JOIN 測試<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);
?>

效果圖:

Image 3.jpg

相關(guān)視頻教程推薦:MySQL 連接的概念和類型