📜  使用MySQl联接

📅  最后修改于: 2020-11-27 06:37:06             🧑  作者: Mango


在前面的章节中,我们一次从一个表中获取数据。这足以满足简单需要,但是在大多数实际的MySQL使用中,您通常需要在单个查询中从多个表中获取数据。

您可以在单个SQL查询中使用多个表。 MySQL中的联接行为是指将两个或多个表粉碎为一个表。

您可以在SELECT,UPDATE和DELETE语句中使用JOINS来联接MySQL表。我们还将看到LEFT JOIN的示例,它与简单的MySQL JOIN不同。

在命令提示符下使用联接

假设我们在TUTORIALS中有两个表tcount_tbltutorials_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      | 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函数mysql_query()中,然后将以通常的方式获取结果。

以下示例-

 ".
         "Count: {$row['tutorial_count']} 
". "Tutorial ID: {$row['tutorial_id']}
". "--------------------------------
"; } echo "Fetched data successfully\n"; mysql_close($conn); ?>

MySQL左联接

MySQL左联接不同于简单联接。 MySQL LEFT JOIN对左侧的表进行了一些额外的考虑。

如果我执行LEFT JOIN ,我将以相同的方式获得所有匹配的记录,并且在连接的左表中,对于每个不匹配的记录,我还将获得一个额外的记录:因此,确保(在我的示例中)每个AUTHOR都获得一个提到。

请尝试以下示例,以了解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中有点复杂,在做实际示例时会更加清楚。