手机
当前位置:查字典教程网 >编程开发 >mysql数据库 >在MySQL中使用JOIN语句进行连接操作的详细教程
在MySQL中使用JOIN语句进行连接操作的详细教程
摘要:到目前,我们已经学习了从一个表中获取数据。这是简单的需要,但在大多数现实MySQL的使用,经常需要将数据从多个表中的一个单一的查询。可以使用...

到目前,我们已经学习了从一个表中获取数据。这是简单的需要,但在大多数现实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 | 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脚本中使用联接:

可以使用任何上述的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 successfullyn"; 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将变得更加清晰。

【在MySQL中使用JOIN语句进行连接操作的详细教程】相关文章:

在MySQL数据库中使用C执行SQL语句的方法

在MySQL数据库中复位根用户的密码的方法

MySQL最常见的操作语句小结

MySQL与MSSQl使用While语句循环生成测试数据的代码

MySQL中使用case when 语句实现多条件查询的方法

在MySQL字段中使用逗号分隔符的方法分享

工作中常用的mysql语句分享 不用php也可以实现的效果

MySQL 5.0触发器参考教程第1/4页

将MySQL从MyISAM转换成InnoDB错误和解决办法

MySQL五个查询优化方法

精品推荐
分类导航