1. 程式人生 > >吳裕雄 19-Mysql 連線的使用

吳裕雄 19-Mysql 連線的使用

JOIN 按照功能大致分為如下三類:
INNER JOIN(內連線,或等值連線):獲取兩個表中欄位匹配關係的記錄。
LEFT JOIN(左連線):獲取左表所有記錄,即使右表沒有對應匹配的記錄。
RIGHT JOIN(右連線): 與 LEFT JOIN 相反,用於獲取右表所有記錄,即使左表沒有對應匹配的記錄。

DROP TABLE IF EXISTS `runoob_tbl`;
CREATE TABLE `runoob_tbl` (
`runoob_id` int(11) NOT NULL AUTO_INCREMENT,
`runoob_title` varchar(100) NOT NULL,
`runoob_author` varchar(40) NOT NULL,
`submission_date` date DEFAULT NULL,
PRIMARY KEY (`runoob_id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;
BEGIN;
INSERT INTO `runoob_tbl` VALUES
('1', '學習 PHP', '菜鳥教程', '2017-04-12'),
('2', '學習 MySQL', '菜鳥教程', '2017-04-12'),
('3', '學習 Java', 'RUNOOB.COM', '2015-05-01'),
('4', '學習 Python', 'RUNOOB.COM', '2016-03-06'),
('5', '學習 C', 'FK', '2017-04-05');
COMMIT;
DROP TABLE IF EXISTS `tcount_tbl`;
CREATE TABLE `tcount_tbl` (
`runoob_author` varchar(255) NOT NULL DEFAULT '',
`runoob_count` int(11) NOT NULL DEFAULT '0'
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
BEGIN;
INSERT INTO `tcount_tbl` VALUES
('菜鳥教程', '10'),
('RUNOOB.COM ', '20'),
('Google', '22');
COMMIT;
SET FOREIGN_KEY_CHECKS = 1;
SELECT * FROM tcount_tbl;
+---------------+--------------+
| runoob_author | runoob_count |
+---------------+--------------+
| 菜鳥教程 | 10 |
| RUNOOB.COM | 20 |
| Google | 22 |
+---------------+--------------+
SELECT * from runoob_tbl;
+-----------+--------------+---------------+-----------------+
| runoob_id | runoob_title | runoob_author | submission_date |
+-----------+--------------+---------------+-----------------+
| 1 | 學習 PHP | 菜鳥教程 | 2017-04-12 |
| 2 | 學習 MySQL | 菜鳥教程 | 2017-04-12 |
| 3 | 學習 Java | RUNOOB.COM | 2015-05-01 |
| 4 | 學習 Python | RUNOOB.COM | 2016-03-06 |
| 5 | 學習 C | FK | 2017-04-05 |
+-----------+--------------+---------------+-----------------+

接下來我們就使用MySQL的INNER JOIN(也可以省略 INNER 使用 JOIN,效果一樣)
來連線以上兩張表來讀取runoob_tbl表中所有runoob_author欄位在tcount_tbl
表對應的runoob_count欄位值:
SELECT a.runoob_id, a.runoob_author, b.runoob_count
FROM runoob_tbl a INNER JOIN tcount_tbl b ON a.runoob_author = b.runoob_author;
+-----------+---------------+--------------+
| runoob_id | runoob_author | runoob_count |
+-----------+---------------+--------------+
| 1 | 菜鳥教程 | 10 |
| 2 | 菜鳥教程 | 10 |
| 3 | RUNOOB.COM | 20 |
| 4 | RUNOOB.COM | 20 |
+-----------+---------------+--------------+
以上 SQL 語句等價於:
SELECT a.runoob_id, a.runoob_author, b.runoob_count
FROM runoob_tbl a, tcount_tbl b WHERE a.runoob_author = b.runoob_author;
+-----------+---------------+--------------+
| runoob_id | runoob_author | runoob_count |
+-----------+---------------+--------------+
| 1 | 菜鳥教程 | 10 |
| 2 | 菜鳥教程 | 10 |
| 3 | RUNOOB.COM | 20 |
| 4 | RUNOOB.COM | 20 |
+-----------+---------------+--------------+

MySQL LEFT JOIN
MySQL left join 與 join 有所不同。 MySQL LEFT JOIN
會讀取左邊資料表的全部資料,即便右邊表無對應資料。
嘗試以下例項,以 runoob_tbl 為左表,tcount_tbl 為右表,理解 MySQL LEFT JOIN 的應用:
SELECT a.runoob_id, a.runoob_author, b.runoob_count
FROM runoob_tbl a LEFT JOIN tcount_tbl b ON a.runoob_author = b.runoob_author;
+-----------+---------------+--------------+
| runoob_id | runoob_author | runoob_count |
+-----------+---------------+--------------+
| 1 | 菜鳥教程 | 10 |
| 2 | 菜鳥教程 | 10 |
| 3 | RUNOOB.COM | 20 |
| 4 | RUNOOB.COM | 20 |
| 5 | FK | NULL |
+-----------+---------------+--------------+

MySQL RIGHT JOIN
MySQL RIGHT JOIN 會讀取右邊資料表的全部資料,即便左邊邊表無對應資料。
例項
嘗試以下例項,以 runoob_tbl 為左表,tcount_tbl 為右表,理解MySQL RIGHT JOIN的應用:
SELECT a.runoob_id, a.runoob_author, b.runoob_count
FROM runoob_tbl a RIGHT JOIN tcount_tbl b ON a.runoob_author = b.runoob_author;
+-----------+---------------+--------------+
| runoob_id | runoob_author | runoob_count |
+-----------+---------------+--------------+
| 1 | 菜鳥教程 | 10 |
| 2 | 菜鳥教程 | 10 |
| 3 | RUNOOB.COM | 20 |
| 4 | RUNOOB.COM | 20 |
| NULL | NULL | 22 |
+-----------+---------------+--------------+
以上例項中使用了 RIGHT JOIN,該語句會讀取右邊的資料表 tcount_tbl
的所有選取的欄位資料,即便在左側表 runoob_tbl 中沒有對應的runoob_author 欄位值。

在 PHP 指令碼中使用 JOIN
PHP 中使用 mysqli_query() 函式來執行 SQL 語句,
你可以使用以上的相同的 SQL 語句作為 mysqli_query() 函式的引數。
嘗試如下例項:
<?php
$dbhost = 'localhost:3306'; // mysql伺服器主機地址
$dbuser = 'root'; // mysql使用者名稱
$dbpass = '123456'; // mysql使用者名稱密碼
$conn = mysqli_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
die('連線失敗: ' . mysqli_error($conn));
}
// 設定編碼,防止中文亂碼
mysqli_query($conn , "set names utf8");

$sql = 'SELECT a.runoob_id, a.runoob_author, b.runoob_count FROM runoob_tbl a INNER JOIN tcount_tbl b ON a.runoob_author = b.runoob_author';

mysqli_select_db( $conn, 'RUNOOB' );
$retval = mysqli_query( $conn, $sql );
if(! $retval )
{
die('無法讀取資料: ' . mysqli_error($conn));
}
echo '<h2>菜鳥教程 MySQL JOIN 測試<h2>';
echo '<table border="1"><tr><td>教程 ID</td><td>作者</td><td>登陸次數</td></tr>';
while($row = mysqli_fetch_array($retval, MYSQL_ASSOC))
{
echo "<tr><td> {$row['runoob_id']}</td> ".
"<td>{$row['runoob_author']} </td> ".
"<td>{$row['runoob_count']} </td> ".
"</tr>";
}
echo '</table>';
mysqli_close($conn);
?>