发布于 2015-07-31 16:31:08 | 331 次阅读 | 评论: 0 | 来源: 网络整理

到目前为止,我们只是从一个表读取数据。这是相对简单的,但在大多数现实中的MySQL使用,需要从多个表中,在单个查询获得数据。

可以在单个SQL查询中使用多个表。连接MySQL中的行在两个或多个表到一个表。

可以使用Join在SELECT,UPDATE和DELETE语句加入MySQL表。我们将看到LEFT JOIN的例子, 这与简单的MySQL JOIN有所不同。

1、在命令提示符中使用联接

假设我们有两个表 tcount_tbl 和 tutorials_tbl,在数据库:test ,完整列表如下:

示例

试试下面的例子:


root@host# mysql -u root -p password;
Enter password:
mysql> use test;
Database changed
mysql> SELECT * FROM tcount_tbl;
+-----------------+----------------+
| tutorial_author | tutorial_count |
+-----------------+----------------+
| mahran          |             20 |
| mahnaz          |           NULL |
| Jen             |           NULL |
| Gill            |             20 |
| John Poul       |              1 |
| Sanjay          |              1 |
+-----------------+----------------+
6 rows in set (0.01 sec)
mysql> SELECT * from tutorials_tbl;
+-------------+----------------+-----------------+-----------------+
| tutorial_id | tutorial_title | tutorial_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>

现在,我们可以写一个SQL查询来连接这两个表。此查询将从表 tutorials_tbl 和 tcount_tbl 选择所有作者的教程数量。


mysql> SELECT a.tutorial_id, a.tutorial_author, b.tutorial_count
    -> FROM tutorials_tbl a, tcount_tbl b
    -> WHERE a.tutorial_author = b.tutorial_author;
+-------------+-----------------+----------------+
| tutorial_id | tutorial_author | tutorial_count |
+-------------+-----------------+----------------+
|           1 | John Poul       |              1 |
|           3 | Sanjay          |              1 |
+-------------+-----------------+----------------+
2 rows in set (0.01 sec)
mysql>

在PHP脚本使用Join连接

可以使用上述的任何SQL查询在PHP脚本中。只需要传递SQL查询到PHP的mysql_query()函数,然后以通常的方式获取结果。

示例

试试下面的例子:


<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
  die('Could not connect: ' . mysql_error());
}
$sql = 'SELECT a.tutorial_id, a.tutorial_author, b.tutorial_count
        FROM tutorials_tbl a, tcount_tbl b
        WHERE a.tutorial_author = b.tutorial_author';

mysql_select_db('test');
$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['tutorial_author']}  <br> ".
         "Count: {$row['tutorial_count']} <br> ".
         "Tutorial ID: {$row['tutorial_id']} <br> ".
         "--------------------------------<br>";
} 
echo "Fetched data successfullyn";
mysql_close($conn);
?>

MySQL LEFT JOIN

MySQL左连接不同于简单连接。MySQL LEFT JOIN提供该表额外字段在左侧。

如果使用LEFT JOIN,得到的所有记录的匹配方式相同,在左边表中得到的每个记录不匹配也会有一个额外的记录。 从而确保(在本例子),每次作者信息都会列出:

示例

试试下面的例子就明白了 LEFT JOIN 的用法(注:第2条,作者Abdul S没有写教程也被列出来了):


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

需要做更多的练习,以熟悉连接。这个概念是有点复杂,做这样实际例子,在MySQL/SQL将变得更加清晰。

最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务