当前位置:Gxlcms > 数据库问题 > Mysql Join

Mysql Join

时间:2021-07-01 10:21:17 帮助过:3人阅读

接下来我们就使用MySQL的JOIN来连接以上两张表来读取tutorials_tbl表中所有tutorial_author字段在tcount_tbl表对应的tutorial_count字段值:

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脚本中使用JOIN

PHP 中使用mysql_query()函数来执行SQL语句,你可以使用以上的相同的SQL语句作为mysql_query()函数的参数。

尝试如下实例:

<?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 LEFT JOIN

MySQL left join 与 join 有所不同。 MySQL LEFT JOIN 会读取左边数据表的全部数据,即便右边表无对应数据。

实例

尝试以下实例,理解MySQL 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)

以上实例中使用了LEFT JOIN,该语句会读取左边的数据表tutorials_tbl的所有选取的字段数据,即便在右侧表tcount_tbl中没有对应的tutorial_author字段值。

Mysql Join

标签:

人气教程排行