MySQL INNER JOIN clause introduced
MySQL INNER JOIN
clause a matching table rows to other tables in rows and columns to allow the query contains rows from two tables.
INNER JOIN
Clause is SELECT
an optional part of the statement, it appears in the FROM clause after.
In use INNER JOIN
before clause, you must specify the following conditions:
- First, the primary table specified in the FROM clause.
- Secondly, the main table table you want to connect should appear in the
INNER JOIN
clause. In theory, you can connect multiple other tables. However, in order to obtain better performance, you should limit the number of tables to be connected (preferably not more than three tables). - Third, the connection conditions, or join predicates. Join condition appears in the
INNER JOIN
clauseON
after the keyword. That is connected to the main rule matching rows in the table with the other row in the table.
INNER JOIN
Clause syntax is as follows:
SELECT column_list
FROM t1
INNER JOIN t2 ON join_condition1
INNER JOIN t3 ON join_condition2
...
WHERE where_conditions;
SQL
Assuming INNER JOIN
clause joins two tables: t1
and t2
, let's simplify the syntax above.
SELECT column_list
FROM t1
INNER JOIN t2 ON join_condition;
SQL
For t1
each row in the table, INNER JOIN
clause it t2
compares each row of the table, to see if they satisfy the join condition. When the connection condition is satisfied, INNER JOIN
the return of t1
and t2
a new row in the table columns.
Please note, t1
and t2
rows in the table must be matched according to the connection conditions. If no match is found, the query returns an empty result set. When the connection over 2
time tables, also apply to this logic.
The following Venn diagram illustrates INNER JOIN
the principle clause. The result set must appear in two rows in the table: t1
and t2
cross section, as shown in two circles -
Avoid listed in error in MySQL INNER JOIN
If connecting a plurality of tables with the same column name, you must use a reference table qualifier SELECT
and ON
clauses column, the column to avoid errors.
For example, if t1
and t2
table has named c
a same column name must SELECT
and ON
clause table qualifier, such as used t1.c
or t2.c
specified in the table that is referenced in the c
column.
In order to save time writing table qualifier, you can use table aliases in the query. For example, you can name a long verylonglonglong_tablename
list use table aliases, and use t.column
references that column, instead of using verylonglonglong_tablename.column
, but if you like to write or use such a long table name, you should also allow your development partner call you a few similar: sucker - such as this if!
MySQL INNER JOIN Example
Let's look at the sample database (yiibaidb) the product ( products
) and product lines ( productlines
) table. Their ER FIG follows -
In the above figures, products
tables productLine
listed references productlines
table productline
columns. products
Table productLine
column called the foreign key column.
Generally, the connector having a foreign key relationship table, such as the product line ( productlines
) and the product ( products
) table. Now, if you want to obtain the following data -
- Get
products
tableproductCode
andproductName
column values. - Get
productlines
description product line -textDescription
the column.
For this purpose, through the use INNER JOIN
of Clause productline
to select rows string matching the query data from the two tables, as shown below:
SELECT
productCode,
productName,
textDescription
FROM
products t1
INNER JOIN
productlines t2 ON t1.productline = t2.productline;
SQL
Query execution above, following results (part) -
Since the table is connected to two columns a column using the same: productline
and thus can use the following syntax:
SELECT
productCode,
productName,
textDescription
FROM
products
INNER JOIN
productlines USING (productline);
SQL
The above statement returns the same result set, but using this syntax, without the use of an alias table.
MySQL INNER JOIN GROUP BY子句
See the following orders and order details table, orders
the table and orderdetails
the table structure is as follows -
Possible to use a GROUP BY clause INNER JOIN
clause from orders
and orderdetails
obtaining order number, order status, and total sales table, as follows:
SELECT
T1.orderNumber,
status,
SUM(quantityOrdered * priceEach) total
FROM
orders AS T1
INNER JOIN
orderdetails AS T2 ON T1.orderNumber = T2.orderNumber
GROUP BY orderNumber;
SQL
Performing the above query, the results shown below (Part) -
Similarly, the following query statement to get the same results as described above:
SELECT
orderNumber,
status,
SUM(quantityOrdered * priceEach) total
FROM
orders
INNER JOIN
orderdetails USING (orderNumber)
GROUP BY orderNumber;
SQL
MySQL INNER JOIN use other than equality operator
So far, you've seen the connection using an equal predicate operator ( =
) to match the line. But more can be used ( >
), less than ( <
) and not equal to ( <>
) other operators operators to form the join predicate.
The following query uses less than ( <
) connection to find the code below the S10_1678
selling price of the product manufacturer's suggested retail price ( MSRP
) of all products.
SELECT
orderNumber,
productName,
msrp,
priceEach
FROM
products p
INNER JOIN
orderdetails o ON p.productcode = o.productcode
AND p.msrp > o.priceEach
WHERE
p.productcode = 'S10_1678';
SQL
Implementation of the above query, the following output -
mysql> SELECT
orderNumber,
productName,
msrp,
priceEach
FROM
products p
INNER JOIN
orderdetails o ON p.productcode = o.productcode
AND p.msrp > o.priceEach
WHERE
p.productcode = 'S10_1678';
+-------------+---------------------------------------+------+-----------+
| orderNumber | productName | msrp | priceEach |
+-------------+---------------------------------------+------+-----------+
| 10107 | 1969 Harley Davidson Ultimate Chopper | 95.7 | 81.35 |
| 10121 | 1969 Harley Davidson Ultimate Chopper | 95.7 | 86.13 |
| 10134 | 1969 Harley Davidson Ultimate Chopper | 95.7 | 90.92 |
| 10145 | 1969 Harley Davidson Ultimate Chopper | 95.7 | 76.56 |
| 10159 | 1969 Harley Davidson Ultimate Chopper | 95.7 | 81.35 |
| 10168 | 1969 Harley Davidson Ultimate Chopper | 95.7 | 94.74 |
| 10399 | 1969 Harley Davidson Ultimate Chopper | 95.7 | 77.52 |
| 10403 | 1969 Harley Davidson Ultimate Chopper | 95.7 | 85.17 |
... ...
| 10417 | 1969 Harley Davidson Ultimate Chopper | 95.7 | 79.43 |
+-------------+---------------------------------------+------+-----------+
26 rows in set
From the original [EBEY tutorial], the original link: https://www.yiibai.com/mysql/inner-join.html