在前几章节中,我们已经学会了如果在一张表中读取数据,这是相对简单的,但是在真正的应用中经常需要从多个数据表中读取数据。
本章节我们将向大家介绍如何使用 MySQL 的 JOIN 在两个或多个表中查询数据。
你可以在SELECT, UPDATE 和 DELETE 语句中使用 Mysql 的 JOIN 来联合多表查询。
JOIN 按照功能大致分为如下三类:
INNER JOIN(内连接,或等值连接)
:获取两个表中字段匹配关系的记录。
LEFT JOIN(左连接):
获取左表所有记录,即使右表没有对应匹配的记录。
RIGHT JOIN(右连接):
与 LEFT JOIN 相反,用于获取右表所有记录,即使左表没有对应匹配的记录。
本章节使用的数据库结构及数据下载:web3.sql。
我们在RUNOOB数据库中有两张表 tcount_tbl 和 web3_tbl。两张数据表数据如下:
尝试以下实例:
root@host# mysql -u root -p password; |
Enter password:******* |
mysql> use RUNOOB; |
Database changed |
mysql> SELECT * FROM tcount_tbl; |
+-----------------+----------------+ |
| web3_author | web3_count | |
+-----------------+----------------+ |
| mahran | 20 | |
| mahnaz | NULL | |
| Jen | NULL | |
| Gill | 20 | |
| John Poul | 1 | |
| Sanjay | 1 | |
+-----------------+----------------+ |
6 rows in set (0.01 sec) |
mysql> SELECT * from web3_tbl; |
+-------------+----------------+-----------------+-----------------+ |
| web3_id | web3_title | web3_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,效果一样)
来连接以上两张表来读取web3_tbl表中所有web3_author字段在tcount_tbl表对应的web3_count字段值:
mysql> SELECT a.web3_id, a.web3_author, b.web3_count FROM web3_tbl a INNER JOIN tcount_tbl b ON a.web3_author = b.web3_author; |
+-----------+---------------+--------------+ |
| web3_id | web3_author | web3_count | |
+-----------+---------------+--------------+ |
| 1 | John Poul | 1 | |
| 3 | Sanjay | 1 | |
+-----------+---------------+--------------+ |
2 rows in set (0.00 sec) |
mysql> SELECT a.web3_id, a.web3_author, b.web3_count FROM web3_tbl a, tcount_tbl b WHERE a.web3_author = b.web3_author; |
+-------------+-----------------+----------------+ |
| web3_id | web3_author | web3_count | |
+-------------+-----------------+----------------+ |
| 1 | John Poul | 1 | |
| 3 | Sanjay | 1 | |
+-------------+-----------------+----------------+ |
2 rows in set (0.01 sec) |
mysql> |
MySQL left join 与 join 有所不同。 MySQL LEFT JOIN 会读取左边数据表的全部数据,即便右边表无对应数据。
尝试以下实例,以 web3_tbl
为左表,tcount_tbl
为右表,理解MySQL LEFT JOIN的应用:
root@host# mysql -u root -p password; |
Enter password:******* |
mysql> use RUNOOB; |
Database changed |
mysql> SELECT a.web3_id, a.web3_author, b.web3_count FROM web3_tbl a LEFT JOIN tcount_tbl b ON a.web3_author = b.web3_author; |
+-------------+-----------------+----------------+ |
| web3_id | web3_author | web3_count | |
+-------------+-----------------+----------------+ |
| 1 | John Poul | 1 | |
| 2 | Abdul S | NULL | |
| 3 | Sanjay | 1 | |
+-------------+-----------------+----------------+ |
3 rows in set (0.02 sec) |
以上实例中使用了LEFT JOIN,该语句会读取左边的数据表web3_tbl的所有选取的字段数据,即便在右侧表tcount_tbl中没有对应的web3_author字段值。
MySQL RIGHT JOIN 会读取右边数据表的全部数据,即便左边边表无对应数据。
尝试以下实例,以 tcount_tbl
为左表,web3_tbl
为右表,理解MySQL RIGHT JOIN的应用:
root@host# mysql -u root -p password; |
Enter password:******* |
mysql> use RUNOOB; |
Database changed |
mysql> SELECT b.web3_id, b.web3_author, a.web3_count FROM tcount_tbl a RIGHT JOIN web3_tbl b ON a.web3_author = b.web3_author; |
+-------------+-----------------+----------------+ |
| web3_id | web3_author | web3_count | |
+-------------+-----------------+----------------+ |
| 1 | John Poul | 1 | |
| 2 | Abdul S | NULL | |
| 3 | Sanjay | 1 | |
+-------------+-----------------+----------------+ |
3 rows in set (0.02 sec) |
PHP 中使用mysql_query()函数来执行SQL语句,你可以使用以上的相同的SQL语句作为mysql_query()函数的参数。
尝试如下实例:
<?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.web3_id, a.web3_author, b.web3_count FROM web3_tbl a INNER JOIN tcount_tbl b ON a.web3_author = b.web3_author'; |
mysql_select_db('RUNOOB'); |
$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['web3_author']} <br> ". |
"Count: {$row['web3_count']} <br> ". |
"Tutorial ID: {$row['web3_id']} <br> ". |
"--------------------------------<br>"; |
} |
echo "Fetched data successfully\n"; |
mysql_close($conn); |
?> |