English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

MySQL Use of Joins

In the previous chapters, we have learned how to read data from a single table, which is relatively simple, but in real applications, it is often necessary to read data from multiple data tables.

In this chapter, we will introduce how to use MySQL's JOIN to query data in two or more tables.

You can use JOIN of Mysql in SELECT, UPDATE, and DELETE statements to combine multiple table queries.

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

  • INNER JOIN (Inner Join, or Equi-Join):To obtain records with matching field relationships in the two tables.

  • LEFT JOIN (Left Join):It retrieves all records from the left table, even if there is no matching record in the right table.

  • RIGHT JOIN (Right Join): On the contrary to LEFT JOIN, it is used to obtain all records from the right table, even if there is no matching record in the left table.

Database structure and data download used in this chapter:w3codebox-mysql-join-test.sql.

Use INNER JOIN in the command prompt

We are in w3There are two tables, tcount_tbl and w in the codebox database.3codebox_tbl. The data of two tables in codebox is as follows:

Online example

Try the following example:

mysql> use w3codebox;
Database changed
mysql> SELECT * FROM tcount_tbl;
+---------------+--------------+
| w3codebox_author | w3codebox_count |
+---------------+--------------+
| Basic Tutorial Website  | 10           |
| oldtoolbag.com    | 20           |
| Google        | 22           |
+---------------+--------------+
3 rows in set (0.01 sec)
 
mysql> SELECT * from w3codebox_tbl;
+-----------+---------------+---------------+-----------------+
| w3codebox_id | w3codebox_title  | w3codebox_author | submission_date |
+-----------+---------------+---------------+-----------------+
| 1         | Learn PHP    | Basic Tutorial Website  | 2017-04-12      |
| 2         | Learn MySQL  | Basic Tutorial Website  | 2017-04-12      |
| 3         | Learn Java   | oldtoolbag.com    | 2015-05-01      |
| 4         | Learn Python | oldtoolbag.com    | 2016-03-06      |
| 5         | Learn C      | FK            | 2017-04-05      |
+-----------+---------------+---------------+-----------------+
5 rows in set (0.01 sec)

Next, we will use MySQL'sINNER JOIN (can also omit INNER and use JOIN, the effect is the same)to connect the above two tables to read w3all w in codebox_tbl table3codebox_author field in tcount_tbl table corresponds to w3codebox_count field value:

mysql> SELECT a.w3codebox_id, a.w3codebox_author, b.w3codebox_count FROM w3codebox_tbl a INNER JOIN tcount_tbl b ON a.w3codebox_author = b.w3codebox_author;
+-------------+-----------------+----------------+
| a.w3codebox_id | a.w3codebox_author | b.w3codebox_count |
+-------------+-----------------+----------------+
| 1           | Basic Tutorial Website    | 10             |
| 2           | Basic Tutorial Website    | 10             |
| 3           | oldtoolbag.com      | 20             |
| 4           | oldtoolbag.com      | 20             |
+-------------+-----------------+----------------+
4 rows in set (0.00 sec)

The above SQL statement is equivalent to:

mysql> SELECT a.w3codebox_id, a.w3codebox_author, b.w3codebox_count FROM w3codebox_tbl a, tcount_tbl b WHERE a.w3codebox_author = b.w3codebox_author;
+-------------+-----------------+----------------+
| a.w3codebox_id | a.w3codebox_author | b.w3codebox_count |
+-------------+-----------------+----------------+
| 1           | Basic Tutorial Website    | 10             |
| 2           | Basic Tutorial Website    | 10             |
| 3           | oldtoolbag.com      | 20             |
| 4           | oldtoolbag.com      | 20             |
+-------------+-----------------+----------------+
4 rows in set (0.01 sec)

MySQL LEFT JOIN

MySQL left join is different from join. MySQL LEFT JOIN will read all the data from the left table, even if there is no corresponding data in the right table.

Online example

Try the following example to w3codebox_tbl For the left table,tcount_tbl For the right table, understand the application of MySQL LEFT JOIN:

mysql> SELECT a.w3codebox_id, a.w3codebox_author, b.w3codebox_count FROM w3codebox_tbl a LEFT JOIN tcount_tbl b ON a.w3codebox_author = b.w3codebox_author;
+-------------+-----------------+----------------+
| a.w3codebox_id | a.w3codebox_author | b.w3codebox_count |
+-------------+-----------------+----------------+
| 1           | Basic Tutorial Website    | 10             |
| 2           | Basic Tutorial Website    | 10             |
| 3           | oldtoolbag.com      | 20             |
| 4           | oldtoolbag.com      | 20             |
| 5           | FK              | NULL           |
+-------------+-----------------+----------------+
5 rows in set (0.01 sec)

In the above example, LEFT JOIN is used, which will read the left table w3all the selected fields of codebox_tbl, even if there is no corresponding w in the right table tcount_tbl.3codebox_author field value.

MySQL RIGHT JOIN

MySQL RIGHT JOIN will read all the data from the right table, even if there is no corresponding data in the left table.

Online example

Try the following example to w3codebox_tbl For the left table,tcount_tbl For the right table, understand the application of MySQL RIGHT JOIN:

mysql> SELECT a.w3codebox_id, a.w3codebox_author, b.w3codebox_count FROM w3codebox_tbl a RIGHT JOIN tcount_tbl b ON a.w3codebox_author = b.w3codebox_author;
+-------------+-----------------+----------------+
| a.w3codebox_id | a.w3codebox_author | b.w3codebox_count |
+-------------+-----------------+----------------+
| 1           | Basic Tutorial Website    | 10             |
| 2           | Basic Tutorial Website    | 10             |
| 3           | oldtoolbag.com      | 20             |
| 4           | oldtoolbag.com      | 20             |
| NULL        | NULL            | 22             |
+-------------+-----------------+----------------+
5 rows in set (0.01 sec)

In the above example, RIGHT JOIN is used, which will read all the selected field data from the right table tcount_tbl, even if there is no match in the left table w3codebox_tbl does not have a corresponding w3codebox_author field value.

Using JOIN in PHP scripts

In PHP scripts, use the mysqli_query() function to execute SQL statements. You can use the same SQL statement as the parameter of mysqli_query() function.

Try the following example:

<?php
$dbhost = 'localhost';  // MySQL server host address
$dbuser = 'root';            // MySQL username
$dbpass = '123456';          // MySQL username and password
$conn = mysqli_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
    die('Connection failed: ' . mysqli_error($conn));
}
// Set encoding to prevent Chinese garbled characters
mysqli_query($conn , "set names utf8");
 
$sql = 'SELECT a.w3codebox_id, a.w3codebox_author, b.w3codebox_count FROM w3codebox_tbl a INNER JOIN tcount_tbl b ON a.w3codebox_author = b.w3codebox_author';
 
mysqli_select_db($conn, 'w}}3codebox');
$retval = mysqli_query($conn, $sql);
if(! $retval)
{
    die('Unable to read data: ' . mysqli_error($conn));
}
echo '<h2">Basic Tutorial Website MySQL JOIN Test<h2">
echo '<table border="1><tr><td>Tutorial ID</td><td>Author</td><td>Login Times</td></tr>';
while($row = mysqli_fetch_array($retval, MYSQLI_ASSOC))
{
    echo "<tr><td> "{$row['w3codebox_id']}</td> ".
         "<td>{$row['w3codebox_author']} </td> ".
         "<td>{$row['w3codebox_count'} </td> ".
         "</tr>";
}
echo '</table>';
mysqli_close($conn);
?>

The output result is shown as follows: