The use of Mysql connection JOIN

This article describes how to use MySQL's JOIN to query data in two or more tables.

JOIN is roughly divided into the following three categories according to its function:

  • INNER JOIN (inner join, or equal join) : Get records with matching fields in two tables.
  • LEFT JOIN (left join): Get all records in the left table, even if there are no matching records in the right table.
  • RIGHT JOIN (right join):  In contrast to LEFT JOIN, it is used to get all records in the right table, even if there are no corresponding matching records in the left table.

 

Using INNER JOIN in Command Prompt

Create two tables tcount_tbl and runoob_tbl in the RUNOOB database. The data of the two data tables are as follows:

MariaDB [RUNOOB]> SELECT * from runoob_tbl;
+-----------+---------------+---------------+-----------------+
| runoob_id | runoob_title  | runoob_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)

MariaDB [RUNOOB]> SELECT * FROM tcount_tbl;
+---------------+--------------+
| runoob_author | runoob_count |
+---------------+--------------+
| mahran        |           20 |
| mahran        |         NULL |
| Jen | NULL |
| Gill          |           20 |
| John Poul      | 1 |
| Sanjay        |            1 |
+---------------+--------------+
6 rows in set (0.00 sec)

Next, use MySQL's INNER JOIN (you can also omit INNER and use JOIN, the effect is the same) to connect the above two tables to read the runoob_count field value corresponding to all runoob_author fields in the runoob_tbl table in the tcount_tbl table:

MariaDB [RUNOOB]> SELECT a.runoob_id, a.runoob_author, b.runoob_count FROM runoob_tbl aINNER JOINtcount_tbl bON a.runoob_author = b.runoob_author;
+-----------+---------------+--------------+
| runoob_id | runoob_author | runoob_count |
+-----------+---------------+--------------+
|         1 | John Poul     |            1 
|         3 | Sanjay        |            1 |
+-----------+---------------+--------------+
2 rows in set (0.00 sec)

以上 SQL 语句等价于:

MariaDB [RUNOOB]> SELECT a.runoob_id, a.runoob_author, b.runoob_count FROM runoob_tbl a, tcount_tbl bWHERE a.runoob_author = b.runoob_author;
+-----------+---------------+--------------+
| runoob_id | runoob_author | runoob_count |
+-----------+---------------+--------------+
|         1 | John Poul     |            1 |
|         3 | Sanjay        |            1 |
+-----------+---------------+--------------+
2 rows in set (0.00 sec)

 


在命令提示符中使用 LEFT JOIN

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

以 runoob_tbl 为左表,tcount_tbl 为右表,掌握MySQL LEFT JOIN的应用:

MariaDB [RUNOOB]> SELECT a.runoob_id, a.runoob_author, b.runoob_count FROM runoob_tbl aLEFT JOINtcount_tbl bON a.runoob_author = b.runoob_author;
+-----------+---------------+--------------+
| runoob_id | runoob_author | runoob_count |
+-----------+---------------+--------------+
|         1 | John Poul     |            1 |
|         3 | Sanjay        |            1 |
|         2 | Abdul S       |         NULL |
+-----------+---------------+--------------+
3 rows in set (0.00 sec)

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

 


在命令提示符中使用 RIGHT JOIN

MySQL RIGHT JOIN 会读取右边数据表的全部数据,即便左边边表无对应数据。

以 tcount_tbl 为左表,runoob_tbl 为右表,掌握MySQL RIGHT JOIN的应用:

MariaDB [RUNOOB]> SELECT b.runoob_id, b.runoob_author, a.runoob_count FROM tcount_tblaRIGHT JOINrunoob_tbl bON a.runoob_author = b.runoob_author;
+-----------+---------------+--------------+
| runoob_id | runoob_author | runoob_count |
+-----------+---------------+--------------+
|         1 | John Poul     |            1 |
|         3 | Sanjay        |            1 |
|         2 | Abdul S       |         NULL |
+-----------+---------------+--------------+
3 rows in set (0.00 sec)

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


在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.runoob_id, a.runoob_author, b.runoob_count FROM runoob_tbl a INNER JOIN tcount_tbl b ON a.runoob_author = b.runoob_author';
  • mysql_select_db('RUNOOB');
  • $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['runoob_author']} <br> ".
  • "Count: {$row['runoob_count']} <br> ".
  • "Tutorial ID: {$row['runoob_id']} <br> ".
  • "--------------------------------<br>";
  • }
  • echo "Fetched data successfully\n";
  • mysql_close($conn);
  • ?>

 



 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326452524&siteId=291194637