您的位置:首页 > 数据库 > MySQL

在MySQL中使用JOIN语句进行连接操作的详细教程

2015-05-26 00:00 761 查看
到目前,我们已经学习了从一个表中获取数据。这是简单的需要,但在大多数现实MySQL的使用,经常需要将数据从多个表中的一个单一的查询。

可以使用多个表中的单一SQL查询。在MySQL中联接(join)行为是指两个或多个表到一个表中可以使用连接在SELECT,UPDATE和DELETE语句中加入MySQL表。我们将看到一个例子LEFT JOIN简单的MySQL连接。

在命令提示符使用联接:
假设我们两个表的教程tcount_tbl和tutorials_tbl的完整列表如下:

例子:
试试下面的例子:
root@host# mysql -u root -p password;
Enter password:*******
mysql> use TUTORIALS;
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 | J***A 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脚本中使用联接:

可以使用任何上述的SQL查询的PHP脚本。只需要通过PHP函数mysql_query()执行SQL查询,然后用常规方法获取结果。

例子:
试试下面的例子:
<?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.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('TUTORIALS');
$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 successfully\n";
mysql_close($conn);
?>
MySQL左连接:

一个简单的连接和一个MySQL左连接是不同的。一个MySQL LEFT JOIN提供了额外的考虑到在左边的表。

如果做了LEFT JOIN,得到的所有记录以同样的方式相匹配,此外,得到一个额外的记录每个不匹配的记录,在左表中的联接 - 从而保证了每一个作者得到关联(本例子中):

实例:
试试下面的例子就明白了LEFT JOIN:
root@host# mysql -u root -p password;
Enter password:*******
mysql> use TUTORIALS;
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)
需要做更多的实践才能熟悉JOINS。这是一个复杂的概念,在MySQL/SQL将变得更加清晰。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: