MySQL INNER JOIN clause introduced

MySQL INNER JOIN clause introduced

MySQL INNER JOINclause a matching table rows to other tables in rows and columns to allow the query contains rows from two tables.

INNER JOINClause is SELECTan optional part of the statement, it appears in the FROM clause after.

In use INNER JOINbefore 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 JOINclause. 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 JOINclause ONafter the keyword. That is connected to the main rule matching rows in the table with the other row in the table.

INNER JOINClause 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 JOINclause joins two tables: t1and t2, let's simplify the syntax above.

SELECT column_list
FROM t1
INNER JOIN t2 ON join_condition;
SQL

For t1each row in the table, INNER JOINclause it t2compares each row of the table, to see if they satisfy the join condition. When the connection condition is satisfied, INNER JOINthe return of t1and t2a new row in the table columns.

Please note, t1and t2rows 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 2time tables, also apply to this logic.

The following Venn diagram illustrates INNER JOINthe principle clause. The result set must appear in two rows in the table: t1and t2cross section, as shown in two circles -

img

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 SELECTand ONclauses column, the column to avoid errors.

For example, if t1and t2table has named ca same column name must SELECTand ONclause table qualifier, such as used t1.cor t2.cspecified in the table that is referenced in the ccolumn.

In order to save time writing table qualifier, you can use table aliases in the query. For example, you can name a long verylonglonglong_tablenamelist use table aliases, and use t.columnreferences 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 -

img

In the above figures, productstables productLinelisted references productlinestable productlinecolumns. productsTable productLinecolumn 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 productstable productCodeand productNamecolumn values.
  • Get productlinesdescription product line - textDescriptionthe column.

For this purpose, through the use INNER JOINof Clause productlineto 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) -

img

Since the table is connected to two columns a column using the same: productlineand 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, ordersthe table and orderdetailsthe table structure is as follows -

img

Possible to use a GROUP BY clause INNER JOINclause from ordersand orderdetailsobtaining 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) -

img

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_1678selling 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

Guess you like

Origin www.cnblogs.com/ubantu/p/11280429.html