MySQL Using Connections
In the previous chapters, we 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 tables.
In this chapter we will introduce how to use MySQL's JOIN to query data from two or more tables.
You can use MySQL's JOIN in SELECT, UPDATE, and DELETE statements to query multiple tables together.
JOINs are roughly divided into the following three types by function:
- INNER JOIN (inner join, or equi-join): Obtain records with matching field relationships in both tables.
- **LEFT JOIN (left join):** Obtain all records from the left table, even if there are no matching records in the right table.
- RIGHT JOIN (right join): Opposite to LEFT JOIN, used to obtain all records from the right table, even if there are no matching records in the left table.
Download the database structure and data used in this chapter:runoob-mysql-join-test.sql。
INNER JOIN
INNER JOIN returns the matching rows in two tables that satisfy the join condition. The following is the basic syntax of the INNER JOIN statement:
SELECT column1, column2, ...
FROM table1
INNER JOIN table2 ON table1.column_name = table2.column_name;
Parameter Description:
column1,column2, ... are the names of the columns you want to select; if you use*it means select all columns.table1,table2are the names of the two tables to join.table1.column_name = table2.column_nameis the join condition, specifying the columns used for matching in the two tables.
Simple INNER JOIN:
SELECT orders.order_id, customers.customer_name
FROM orders
INNER JOIN customers ON orders.customer_id = customers.customer_id;
The above SQL statement will select the order IDs and customer names from the orders and customers tables that satisfy the join condition.
- Using table aliases:
SELECT o.order_id, c.customer_name
FROM orders AS o
INNER JOIN customers AS c ON o.customer_id = c.customer_id;
The above SQL statement uses the table aliases o and c as aliases for the orders and customers tables.
- Multi-table INNER JOIN:
SELECT orders.order_id, customers.customer_name, products.product_name
FROM orders
INNER JOIN customers ON orders.customer_id = customers.customer_id
INNER JOIN order_items ON orders.order_id = order_items.order_id
INNER JOIN products ON order_items.product_id = products.product_id;
The above SQL statement involves joining four tables: orders, customers, order_items, and products. It selects the order ID, customer name, and product name, joining the related columns of these tables.
- Filtering with a WHERE clause:
SELECT orders.order_id, customers.customer_name
FROM orders
INNER JOIN customers ON orders.customer_id = customers.customer_id
WHERE orders.order_date >= '2023-01-01';
The above SQL statement uses a WHERE clause after the INNER JOIN to filter orders with an order date of '2023-01-01' or later.
LEFT JOIN
LEFT JOIN returns all rows from the left table, including matching rows from the right table. If there are no matching rows in the right table, NULL values will be returned. The following is the basic syntax of the LEFT JOIN statement:
SELECT column1, column2, ...
FROM table1
LEFT JOIN table2 ON table1.column_name = table2.column_name;
- Simple LEFT JOIN:
SELECT customers.customer_id, customers.customer_name, orders.order_id
FROM customers
LEFT JOIN orders ON customers.customer_id = orders.customer_id;
The above SQL statement will select the customer ID and customer name from the customer table, including all rows from the left table customers, as well as matching order IDs (if any).
- Using table aliases:
SELECT c.customer_id, c.customer_name, o.order_id
FROM customers AS c
LEFT JOIN orders AS o ON c.customer_id = o.customer_id;
The above SQL statement uses the table aliases c and o to replace the names of the customers and orders tables, respectively.
- Multi-table LEFT JOIN:
SELECT customers.customer_id, customers.customer_name, orders.order_id, products.product_name
FROM customers
LEFT JOIN orders ON customers.customer_id = orders.customer_id
LEFT JOIN order_items ON orders.order_id = order_items.order_id
LEFT JOIN products ON order_items.product_id = products.product_id;
The above SQL statement joins four tables: customers, orders, order_items, and products, and selects the customer ID, customer name, order ID, and product name. The left join ensures that even if there are no matching rows in order_items or products, the customer and order information will still be returned.
- Filtering with a WHERE clause:
SELECT customers.customer_id, customers.customer_name, orders.order_id
FROM customers
LEFT JOIN orders ON customers.customer_id = orders.customer_id
WHERE orders.order_date >= '2023-01-01' OR orders.order_id IS NULL;
The above SQL statement uses a WHERE clause after the LEFT JOIN to filter orders with an order date of '2023-01-01' or later, as well as customers with no matching orders.
LEFT JOIN is a commonly used join type, especially when you need to return all rows from the left table. When there are no matching rows in the right table, the relevant columns will be displayed as NULL. When using LEFT JOIN, make sure you understand the join conditions and filter the results according to your needs.
RIGHT JOIN
RIGHT JOIN returns all rows from the right table, including matching rows from the left table. If there are no matching rows in the left table, NULL values will be returned. The following is the basic syntax of the RIGHT JOIN statement::
SELECT column1, column2, ...
FROM table1
RIGHT JOIN table2 ON table1.column_name = table2.column_name;
The following is a simple RIGHT JOIN example:
SELECT customers.customer_id, orders.order_id
FROM customers
RIGHT JOIN orders ON customers.customer_id = orders.customer_id;
The above SQL statement will select all order IDs from the right table orders, including matching customer IDs from the left table customers. If there are no matching customer IDs in the customers table, the relevant columns will be displayed as NULL.
During development, RIGHT JOIN is not often used, because it can achieve the same effect using LEFT JOIN with the table order swapped. For example, the above query can be rewritten using LEFT JOIN as:
SELECT customers.customer_id, orders.order_id
FROM orders
LEFT JOIN customers ON orders.customer_id = customers.customer_id;
The above SQL statement returns the same result because LEFT JOIN and RIGHT JOIN are symmetrical. In actual use, you can choose which form to use based on personal preference or organizational conventions.
Using INNER JOIN in the Command Prompt
INNER JOIN can join multiple tables together according to specified join conditions, so you can obtain related data in a query.
When using INNER JOIN, make sure the join conditions are accurate and understand the relationships between the joined tables.
Example
We have two tables, tcount_tbl and runoob_tbl, in the RUNOOB database.
Try the following example:
Test Example Data
mysql
use
RUNOOB
;
Database
changed
mysql
SELECT
FROM
tcount_tbl
; +---------------+--------------+ |
runoob_author
|
runoob_count
| +---------------+--------------+ | Runoob Tutorial |
10
| |
RUNOOB
.
COM
|
20
| |
|
22
| +---------------+--------------+
3
rows
in
set
(
0.01
sec
)
mysql
SELECT
from
runoob_tbl
; +-----------+---------------+---------------+-----------------+ |
runoob_id
|
runoob_title
|
runoob_author
|
submission_date
| +-----------+---------------+---------------+-----------------+ |
1
| Learning
PHP
| Runoob Tutorial |
2017
04
12
| |
2
| Learning
MySQL
| Runoob Tutorial |
2017
04
12
| |
3
| Learning
Java
|
RUNOOB
.
COM
|
2015
05
01
| |
4
| Learning
Python
|
RUNOOB
.
COM
|
2016
03
06
| |
5
| Learning
C
|
FK
|
2017
04
05
| +-----------+---------------+---------------+-----------------+
5
rows
in
set
(
0.01
sec
)
Next we will use MySQL's **INNER JOIN (you can also omit INNER and just use JOIN, with the same effect)** to join the above two tables and read the runoob_count field values in the tcount_tbl table corresponding to all runoob_author fields in the runoob_tbl table:
INNER JOIN
mysql
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
; +-------------+-----------------+----------------+ |
a
.
runoob_id
|
a
.
runoob_author
|
b
.
runoob_count
| +-------------+-----------------+----------------+ |
1
| Runoob Tutorial |
10
| |
2
| Runoob Tutorial |
10
| |
3
|
RUNOOB
.
COM
|
20
| |
4
|
RUNOOB
.
COM
|
20
| +-------------+-----------------+----------------+
4
rows
in
set
(
0.00
sec
)
The above SQL statement is equivalent to:
WHERE Clause
mysql
SELECT
a
.
runoob_id
,
a
.
runoob_author
,
b
.
runoob_count
FROM
runoob_tbl
a
,
tcount_tbl
b
WHERE
a
.
runoob_author
=
b
.
runoob_author
; +-------------+-----------------+----------------+ |
a
.
runoob_id
|
a
.
runoob_author
|
b
.
runoob_count
| +-------------+-----------------+----------------+ |
1
| Runoob Tutorial |
10
| |
2
| Runoob Tutorial |
10
| |
3
|
RUNOOB
.
COM
|
20
| |
4
|
RUNOOB
.
COM
|
20
| +-------------+-----------------+----------------+
4
rows
in
set
(
0.01
sec
)

MySQL LEFT JOIN
LEFT JOIN is a commonly used join type, especially when you need to return all rows from the left table.
When there are no matching rows in the right table, the relevant columns will be displayed as NULL.
When using LEFT JOIN, make sure you understand the join conditions and filter the results according to your needs.
MySQL left join differs from join; LEFT JOIN reads all data from the left table, even if the right table has no corresponding data.
Example
Try the following example, with runoob_tbl as the left table,tcount_tbl as the right table, to understand the application of MySQL LEFT JOIN:
LEFT JOIN
mysql
SELECT
a
.
runoob_id
,
a
.
runoob_author
,
b
.
runoob_count
FROM
runoob_tbl
a
LEFT
JOIN
tcount_tbl
b
ON
a
.
runoob_author
=
b
.
runoob_author
; +-------------+-----------------+----------------+ |
a
.
runoob_id
|
a
.
runoob_author
|
b
.
runoob_count
| +-------------+-----------------+----------------+ |
1
| Runoob Tutorial |
10
| |
2
| Runoob Tutorial |
10
| |
3
|
RUNOOB
.
COM
|
20
| |
4
|
RUNOOB
.
COM
|
20
| |
5
|
FK
|
NULL
| +-------------+-----------------+----------------+
5
rows
in
set
(
0.01
sec
)
The above example uses LEFT JOIN, which reads all selected field data from the left table runoob_tbl, even if there is no corresponding runoob_author field value in the right table tcount_tbl.
MySQL RIGHT JOIN
MySQL RIGHT JOIN reads all data from the right table, even if the left table has no corresponding data.
In actual development, RIGHT JOIN is not often used, because it can achieve the same effect using LEFT JOIN with the table order swapped.
Example
Try the following example, with runoob_tbl as the left table,tcount_tbl as the right table, to understand the application of MySQL RIGHT JOIN:
RIGHT JOIN
mysql
SELECT
a
.
runoob_id
,
a
.
runoob_author
,
b
.
runoob_count
FROM
runoob_tbl
a
RIGHT
JOIN
tcount_tbl
b
ON
a
.
runoob_author
=
b
.
runoob_author
; +-------------+-----------------+----------------+ |
a
.
runoob_id
|
a
.
runoob_author
|
b
.
runoob_count
| +-------------+-----------------+----------------+ |
1
| Runoob Tutorial |
10
| |
2
| Runoob Tutorial |
10
| |
3
|
RUNOOB
.
COM
|
20
| |
4
|
RUNOOB
.
COM
|
20
| |
NULL
|
NULL
|
22
| +-------------+-----------------+----------------+
5
rows
in
set
(
0.01
sec
)
The above example uses RIGHT JOIN, which reads all selected field data from the right table tcount_tbl, even if there is no corresponding runoob_author field value in the left table runoob_tbl.
Using JOIN in a PHP Script
In PHP, the mysqli_query() function is used to execute SQL statements. You can use the same SQL statements above as arguments to the mysqli_query() function.
Try the following example:
MySQL ORDER BY Test:
Runoob Tutorial MySQL JOIN Test' ; echo '
| Tutorial ID | Author | Login Count |
| {$row['runoob_id']} | " . "{$row['runoob_author']} | " . "{$row['runoob_count']} | " . "
The output is shown in the figure below: