INNER JOIN is an operation that joins tables and returns only rows with matching records in the two tables. It is used to correlate records from different tables, filter and obtain specific data, and create complex queries. When using it, you need to specify the connection conditions, that is, the matching columns between the two tables. This operation returns only matching rows, unmatched rows are filtered out.
Usage of INNER JOIN in MySQL
Definition:
INNER JOIN is A query operation that joins two tables and returns only rows with matching records in both tables.
Syntax:
<code class="sql">SELECT column_list FROM table1 INNER JOIN table2 ON table1.column_name = table2.column_name;</code>
Usage:
INNER JOIN is used to retrieve related data from multiple tables. When two tables Used when there are columns in common between records, thereby establishing a relationship and returning only rows with matching records.
Usage scenarios:
Example:
Suppose we have two tables, customers
and orders
:
customers | orders | |
---|---|---|
id | id | |
name | customer_id | |
city | product_name | |
quantity |
To get each customer’s order quantity, we can use INNER JOIN:
<code class="sql">SELECT c.name, COUNT(o.product_name) AS order_count FROM customers c INNER JOIN orders o ON c.id = o.customer_id GROUP BY c.name;</code>
Note points:
The above is the detailed content of How to use inner join in mysql. For more information, please follow other related articles on the PHP Chinese website!