JOIN 按照功能大致分为如下三类:
INNER JOIN(内连接,或等值连接):获取两个表中字段匹配关系的记录。
LEFT JOIN(左连接):获取左表所有记录,即使右表没有对应匹配的记录。
RIGHT JOIN(右连接): 与 LEFT JOIN 相反,用于获取右表所有记录,即使左表没有对应匹配的记录。
在命令提示符中使用 INNER JOIN
我们在sfjvip数据库中有两张表 tcount_tbl 和 sfjvip_tbl。两张数据表数据如下:
示例
测试示例数据
mysql> use sfjvip;
Database changed
mysql> SELECT * FROM tcount_tbl;
+---------------+--------------+
| sfjvip_author | sfjvip_count |
+---------------+--------------+
| sfjvip.COM | 20 |
| Google | 22 |
+---------------+--------------+
3 rows in set (0.01 sec)
mysql> SELECT * from sfjvip_tbl;
+-----------+---------------+---------------+-----------------+
| sfjvip_id | sfjvip_title | sfjvip_author | submission_date |
+-----------+---------------+---------------+-----------------+
| 1 | 学习 Java | sfjvip.COM | 2015-05-01 |
| 2 | 学习 Python | sfjvip.COM | 2016-03-06 |
| 3 | 学习 C | FK | 2017-04-05 |
+-----------+---------------+---------------+-----------------+
5 rows in set (0.01 sec)
接下来我们就使用MySQL的INNER JOIN(也可以省略 INNER 使用 JOIN,效果一样)来连接以上两张表来读取sfjvip_tbl表中所有sfjvip_author字段在tcount_tbl表对应的sfjvip_count字段值:
mysql> SELECT a.sfjvip_id, a.sfjvip_author, b.sfjvip_count FROM sfjvip_tbl a INNER JOIN tcount_tbl b ON a.sfjvip_author = b.sfjvip_author;
+-------------+-----------------+----------------+
| a.sfjvip_id | a.sfjvip_author | b.sfjvip_count |
| 1 | sfjvip.COM | 20 |
| 2 | sfjvip.COM | 20 |
+-------------+-----------------+----------------+
4 rows in set (0.00 sec)
以上 SQL 语句等价于:
mysql> SELECT a.sfjvip_id, a.sfjvip_author, b.sfjvip_count FROM sfjvip_tbl a, tcount_tbl b WHERE a.sfjvip_author = b.sfjvip_author;
+-------------+-----------------+----------------+
| a.sfjvip_id | a.sfjvip_author | b.sfjvip_count |
+-------------+-----------------+----------------+
| 1 | sfjvip.COM | 20 |
| 2 | sfjvip.COM | 20 |
+-------------+-----------------+----------------+
4 rows in set (0.01 sec)
MySQL LEFT JOIN
MySQL left join 与 join 有所不同。 MySQL LEFT JOIN 会读取左边数据表的全部数据,即便右边表无对应数据。
示例
尝试以下示例,以 sfjvip_tbl 为左表,tcount_tbl 为右表,理解 MySQL LEFT JOIN 的应用:
mysql> SELECT a.sfjvip_id, a.sfjvip_author, b.sfjvip_count FROM sfjvip_tbl a LEFT JOIN tcount_tbl b ON a.sfjvip_author = b.sfjvip_author;
+-------------+-----------------+----------------+
| a.sfjvip_id | a.sfjvip_author | b.sfjvip_count |
+-------------+-----------------+----------------+
| 1 | sfjvip.COM | 20 |
| 2 | sfjvip.COM | 20 |
| 3 | FK | NULL |
+-------------+-----------------+----------------+
5 rows in set (0.01 sec)
以上示例中使用了 LEFT JOIN,该语句会读取左边的数据表 sfjvip_tbl 的所有选取的字段数据,即便在右侧表 tcount_tbl中 没有对应的 sfjvip_author 字段值。
MySQL RIGHT JOIN
MySQL RIGHT JOIN 会读取右边数据表的全部数据,即便左边边表无对应数据。
示例
尝试以下示例,以 sfjvip_tbl 为左表,tcount_tbl 为右表,理解MySQL RIGHT JOIN的应用:
mysql> SELECT a.sfjvip_id, a.sfjvip_author, b.sfjvip_count FROM sfjvip_tbl a RIGHT JOIN tcount_tbl b ON a.sfjvip_author = b.sfjvip_author;
+-------------+-----------------+----------------+
| a.sfjvip_id | a.sfjvip_author | b.sfjvip_count |
+-------------+-----------------+----------------+
| 1 | sfjvip.COM | 20 |
| 2 | sfjvip.COM | 20 |
| NULL | NULL | 22 |
+-------------+-----------------+----------------+
5 rows in set (0.01 sec)
以上示例中使用了 RIGHT JOIN,该语句会读取右边的数据表 tcount_tbl 的所有选取的字段数据,即便在左侧表 sfjvip_tbl 中没有对应的sfjvip_author 字段值。
在 PHP 脚本中使用 JOIN
PHP 中使用 mysqli_query() 函数来执行 SQL 语句,你可以使用以上的相同的 SQL 语句作为 mysqli_query() 函数的参数。
示例
<?php
$dbhost = 'localhost'; // 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.sfjvip_id, a.sfjvip_author, b.sfjvip_count FROM sfjvip_tbl a INNER JOIN tcount_tbl b ON a.sfjvip_author = b.sfjvip_author';
mysqli_select_db( $conn, 'sfjvip' );
$retval = mysqli_query( $conn, $sql );
if(! $retval )
{
die('无法读取数据: ' . mysqli_error($conn));
}
echo '<table border="1"><tr><td>教程 ID</td><td>作者</td><td>登陆次数</td></tr>';
while($row = mysqli_fetch_array($retval, MYSQLI_ASSOC))
{
echo "<tr><td> {$row['sfjvip_id']}</td> ".
"<td>{$row['sfjvip_author']} </td> ".
"<td>{$row['sfjvip_count']} </td> ".
"</tr>";
}
echo '</table>';
mysqli_close($conn);
?>