MYSQL Join的使用
栏目:MYSQL 来源:网络 关注:0 时间:2019-08-13
在一张表中读取数据是相对简单的,但是在真正的应用中经常需要从多个数据表中读取数据。
这里我们将向大家介绍如何使用MySQL的JOIN在两个或多个表中查询数据。
你可以在SELECT,UPDATE和DELETE语句中使用MySQL的join来联合多表查询。
以下我们将演示MySQL LEFT JOIN和JOIN的使用的不同之处。
在命令提示符中使用JOIN
我们在RUNOOB数据库中有两张表tcount_tbl和runoob_tbl。两张数据表数据如下:
实例
尝试以下实例:
root@host# MYSQL -u root -p password;
Enter password:*******
MYSQL> use RUNOOB;
Database changed
MYSQL> SELECT * FROM tcount_tbl;
+-----------------+----------------+
| runoob_author | runoob_count |
+-----------------+----------------+
| mahran | 20 |
| mahnaz | NULL |
| Jen | NULL |
| Gill | 20 |
| John Poul | 1 |
| Sanjay | 1 |
+-----------------+----------------+
6 rows in set (0.01 sec)
MYSQL> SELECT * from runoob_tbl;
+-------------+----------------+-----------------+-----------------+
| runoob_id | runoob_title | runoob_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的JOIN来连接以上两张表来读取runoob_tbl表中所有runoob_author字段在tcount_tbl表对应的runoob_count字段值:
MYSQL> 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 | John Poul | 1 |
| 3 | Sanjay | 1 |
+-------------+-----------------+----------------+
2 rows in set (0.01 sec)
MYSQL>
在PHP脚本中使用JOIN
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.runoob_id, a.runoob_author, b.runoob_count
FROM runoob_tbl a, tcount_tbl b
WHERE a.runoob_author = b.runoob_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['runoob_author']} <br> ".
"Count: {$row['runoob_count']} <br> ".
"Tutorial ID: {$row['runoob_id']} <br> ".
"--------------------------------<br>";
}
echo "Fetched data successfully
";
MYSQL_close($conn);
?>
MySQL LEFT JOIN
MySQL left join与join有所不同。MySQL LEFT JOIN会读取左边数据表的全部数据,即便右边表无对应数据。
实例
尝试以下实例,理解MySQL LEFT JOIN的应用:
root@host# MYSQL -u root -p password;
Enter password:*******
MYSQL> use RUNOOB;
Database changed
MYSQL> 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 | John Poul | 1 |
| 2 | Abdul S | NULL |
| 3 | Sanjay | 1 |
+-------------+-----------------+----------------+
3 rows in set (0.02 sec)
以上实例中使用了LEFT JOIN,该语句会读取左边的数据表runoob_tbl的所有选取的字段数据,即便在右侧表tcount_tbl中没有对应的runoob_author字段值。
本文标题:MYSQL Join的使用
本文地址:http://www.q0738.com/mysql/1296.html