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

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

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

在命令提示符使用联接:

假设我们两个表的教程tcount_tbl和tutorials_tbl的完整列表如下:

例子:

试试下面的例子:

[email protected]# 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查询,然后用常规方法获取结果。

例子:

试试下面的例子:

$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‘]}
".

"Count: {$row[‘tutorial_count‘]}
".

"Tutorial ID: {$row[‘tutorial_id‘]}
".

"--------------------------------
";

}

echo "Fetched data successfully\n";

mysql_close($conn);

?>

MySQL左连接:

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

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

实例:

试试下面的例子就明白了LEFT JOIN:

[email protected]# 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将变得更加清晰。

原文:http://www.jb51.net/article/66766.htm

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值