MySQL 联接
-
表联接
在前面的章节中,我们一次从一个表中获取数据。这足以满足简单需要,但在大多数实际的MySQL使用中,您通常需要在单个查询中从多个表中获取数据。您可以在单个SQL查询中使用多个表。MySQL中的联接行为是指将两个或多个表联接为一个表。您可以在SELECT,UPDATE和DELETE语句中使用JOIN来联接MySQL表。我们还将看到LEFT JOIN的示例,它与简单的MySQL JOIN不同。 -
在命令提示符下使用联接
这将使用带有SELECT ORDER BY子句的SQL SELECT命令从MySQL表 – tutorials_tbl中获取数据。例 两个表有以下记录。root@host# mysql -u root -p password; Enter password:******* mysql> use mydb; 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脚本中使用联接
您可以在PHP脚本中使用任何上述SQL查询。您只需要将SQL查询传递到PHP函数mysqli_query()中,然后将以通常的方式获取结果。尝试以下示例
尝试一下<?php $dbhost = 'mysql'; $dbuser = 'test'; $dbpass = '123456'; $conn = mysqli_connect($dbhost, $dbuser, $dbpass); if(! $conn ) { die('Could not connect: ' . mysqli_error($conn)); } $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'; mysqli_select_db($conn,'mydb'); $retval = mysqli_query( $conn,$sql); if(! $retval ) { die('Could not get data: ' . mysqli_error($conn)); } while($row = mysqli_fetch_array($retval, MYSQLI_ASSOC)) { echo "Author:{$row['tutorial_author']} <br> ". "Count: {$row['tutorial_count']} <br> ". "Tutorial ID: {$row['tutorial_id']} <br> ". "--------------------------------<br>"; } echo "Fetched data successfully\n"; mysqli_close($conn); ?>
-
MySQL左联接
MySQL左连接与简单连接不同。MySQL LEFT JOIN对左侧的表进行了一些额外的考虑。如果执行LEFT JOIN,我将以相同的方式获得所有匹配的记录,并且在连接的左表中,对于每个不匹配的记录,我还将获得一个额外的记录:因此,确保(在我的示例中)每个author都获得一条记录。尝试以下示例,以了解LEFT JOIN
尝试一下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)
您需要做更多的练习才能熟悉联接。这在MySQL / SQL中有点复杂,在做实际示例时会变得更加清晰。下面的图例详实的说明了,左联接,右联接,内联接等获取到表记录的集合例如左联接,会获得左表的全部记录。