MySQL 連接的使用

2023-05-09 10:51 更新

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

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

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

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

  • INNER JOIN(內(nèi)連接,或等值連接):獲取兩個(gè)表中字段匹配關(guān)系的記錄。
  • LEFT JOIN(左連接):獲取左表所有記錄,即使右表沒有對(duì)應(yīng)匹配的記錄。
  • RIGHT JOIN(右連接): 與 LEFT JOIN 相反,用于獲取右表所有記錄,即使左表沒有對(duì)應(yīng)匹配的記錄。

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


142233278855062


在命令提示符中使用 INNER JOIN

我們?cè)赪3CSCHOOL數(shù)據(jù)庫中有兩張表 tcount_tbl 和 w3cschool_tbl。兩張數(shù)據(jù)表數(shù)據(jù)如下:

實(shí)例

嘗試以下實(shí)例:

root@host# mysql -u root -p password;
Enter password:*******
mysql> use W3CSCHOOL;
Database changed
mysql> SELECT * FROM tcount_tbl;
+-----------------+----------------+
| w3cschool_author | w3cschool_count |
+-----------------+----------------+
| mahran          |             20 |
| mahnaz          |           NULL |
| Jen             |           NULL |
| Gill            |             20 |
| John Poul       |              1 |
| Sanjay          |              1 |
+-----------------+----------------+
6 rows in set (0.01 sec)
mysql> SELECT * from w3cschool_tbl;
+-------------+----------------+-----------------+-----------------+
| w3cschool_id | w3cschool_title | w3cschool_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>

接下來我們就使用MySQL的INNER JOIN(也可以省略 INNER 使用 JOIN,效果一樣)來連接以上兩張表來讀取w3cschool_tbl表中所有w3cschool_author字段在tcount_tbl表對(duì)應(yīng)的w3cschool_count字段值:

mysql> SELECT a.w3cschool_id, a.w3cschool_author, b.w3cschool_count FROM w3cschool_tbl a INNER JOIN tcount_tbl b ON a.w3cschool_author = b.w3cschool_author;
+-----------+---------------+--------------+
| w3cschool_id | w3cschool_author | w3cschool_count |
+-----------+---------------+--------------+
|         1 | John Poul     |            1 |
|         3 | Sanjay        |            1 |
+-----------+---------------+--------------+
2 rows in set (0.00 sec)

以上 SQL 語句等價(jià)于:

mysql> SELECT a.w3cschool_id, a.w3cschool_author, b.w3cschool_count FROM w3cschool_tbl a, tcount_tbl b WHERE a.w3cschool_author = b.w3cschool_author;
+-------------+-----------------+----------------+
| w3cschool_id | w3cschool_author | w3cschool_count |
+-------------+-----------------+----------------+
|           1 | John Poul       |              1 |
|           3 | Sanjay          |              1 |
+-------------+-----------------+----------------+
2 rows in set (0.01 sec)
mysql>



MySQL LEFT JOIN

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

實(shí)例

嘗試以下實(shí)例,以 w3cschool_tbl 為左表,tcount_tbl 為右表,理解MySQL LEFT JOIN的應(yīng)用:

root@host# mysql -u root -p password;
Enter password:*******
mysql> use W3CSCHOOL;
Database changed
mysql> SELECT a.w3cschool_id, a.w3cschool_author, b.w3cschool_count FROM w3cschool_tbl a LEFT JOIN tcount_tbl b ON a.w3cschool_author = b.w3cschool_author;
+-------------+-----------------+----------------+
| w3cschool_id | w3cschool_author | w3cschool_count |
+-------------+-----------------+----------------+
|           1 | John Poul       |              1 |
|           2 | Abdul S         |           NULL |
|           3 | Sanjay          |              1 |
+-------------+-----------------+----------------+
3 rows in set (0.02 sec)

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



MySQL RIGHT JOIN

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

實(shí)例

嘗試以下實(shí)例,以 tcount_tbl 為左表,w3cschool_tbl 為右表,理解MySQL RIGHT JOIN的應(yīng)用:

root@host# mysql -u root -p password;
Enter password:*******
mysql> use W3CSCHOOL;
Database changed
mysql> SELECT b.w3cschool_id, b.w3cschool_author, a.w3cschool_count FROM tcount_tbl a RIGHT JOIN w3cschool_tbl b ON a.w3cschool_author = b.w3cschool_author;
+-------------+-----------------+----------------+
| w3cschool_id | w3cschool_author | w3cschool_count |
+-------------+-----------------+----------------+
|           1 | John Poul       |              1 |
|           2 | Abdul S         |           NULL |
|           3 | Sanjay          |              1 |
+-------------+-----------------+----------------+
3 rows in set (0.02 sec)

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



在PHP腳本中使用JOIN

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

嘗試如下實(shí)例:

<?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.w3cschool_id, a.w3cschool_author, b.w3cschool_count FROM w3cschool_tbl a INNER JOIN tcount_tbl b ON a.w3cschool_author = b.w3cschool_author';

mysql_select_db('W3CSCHOOL');
$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['w3cschool_author']}  <br> ".
         "Count: {$row['w3cschool_count']} <br> ".
         "Tutorial ID: {$row['w3cschool_id']} <br> ".
         "--------------------------------<br>";
} 
echo "Fetched data successfully\n";
mysql_close($conn);
?>


以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)