Rechercher dans le manuel MySQL
13.2.10.2 JOIN Syntax
MySQL supports the following JOIN syntax for
the table_references part of
SELECT statements and
multiple-table DELETE and
UPDATE statements:
- table_references:
- escaped_table_reference [, escaped_table_reference] ...
- escaped_table_reference:
- table_reference
- | { OJ table_reference }
- table_reference:
- table_factor
- | joined_table
- table_factor:
- | ( table_references )
- joined_table:
- join_specification:
- ON search_condition
- join_column_list:
- column_name [, column_name] ...
- index_hint_list:
- index_hint [, index_hint] ...
- index_hint:
- index_list:
- index_name [, index_name] ...
A table reference is also known as a join expression.
A table reference (when it refers to a partitioned table) may
contain a PARTITION option, including a list
of comma-separated partitions, subpartitions, or both. This
option follows the name of the table and precedes any alias
declaration. The effect of this option is that rows are selected
only from the listed partitions or subpartitions. Any partitions
or subpartitions not named in the list are ignored. For more
information and examples, see
Section 23.5, “Partition Selection”.
The syntax of table_factor is
extended in MySQL in comparison with standard SQL. The standard
accepts only table_reference, not a
list of them inside a pair of parentheses.
This is a conservative extension if each comma in a list of
table_reference items is considered
as equivalent to an inner join. For example:
is equivalent to:
In MySQL, JOIN, CROSS
JOIN, and INNER JOIN are syntactic
equivalents (they can replace each other). In standard SQL, they
are not equivalent. INNER JOIN is used with
an ON clause, CROSS JOIN
is used otherwise.
In general, parentheses can be ignored in join expressions containing only inner join operations. MySQL also supports nested joins. See Section 8.2.1.7, “Nested Join Optimization”.
Index hints can be specified to affect how the MySQL optimizer
makes use of indexes. For more information, see
Section 8.9.4, “Index Hints”. Optimizer hints and the
optimizer_switch system variable are other
ways to influence optimizer use of indexes. See
Section 8.9.3, “Optimizer Hints”, and
Section 8.9.2, “Switchable Optimizations”.
The following list describes general factors to take into account when writing joins:
A table reference can be aliased using
ortbl_nameASalias_nametbl_name alias_name:A
table_subqueryis also known as a derived table or subquery in theFROMclause. See Section 13.2.11.8, “Derived Tables”. Such subqueries must include an alias to give the subquery result a table name, and may optionally include a list of table column names in parentheses. A trivial example follows:INNER JOINand,(comma) are semantically equivalent in the absence of a join condition: both produce a Cartesian product between the specified tables (that is, each and every row in the first table is joined to each and every row in the second table).However, the precedence of the comma operator is less than that of
INNER JOIN,CROSS JOIN,LEFT JOIN, and so on. If you mix comma joins with the other join types when there is a join condition, an error of the formUnknown column 'may occur. Information about dealing with this problem is given later in this section.col_name' in 'on clause'The
search_conditionused withONis any conditional expression of the form that can be used in aWHEREclause. Generally, theONclause serves for conditions that specify how to join tables, and theWHEREclause restricts which rows to include in the result set.If there is no matching row for the right table in the
ONorUSINGpart in aLEFT JOIN, a row with all columns set toNULLis used for the right table. You can use this fact to find rows in a table that have no counterpart in another table:- SELECT left_tbl.*
This example finds all rows in
left_tblwith anidvalue that is not present inright_tbl(that is, all rows inleft_tblwith no corresponding row inright_tbl). See Section 8.2.1.8, “Outer Join Optimization”.The
USING(clause names a list of columns that must exist in both tables. If tablesjoin_column_list)aandbboth contain columnsc1,c2, andc3, the following join compares corresponding columns from the two tables:The
NATURAL [LEFT] JOINof two tables is defined to be semantically equivalent to anINNER JOINor aLEFT JOINwith aUSINGclause that names all columns that exist in both tables.RIGHT JOINworks analogously toLEFT JOIN. To keep code portable across databases, it is recommended that you useLEFT JOINinstead ofRIGHT JOIN.The
{ OJ ... }syntax shown in the join syntax description exists only for compatibility with ODBC. The curly braces in the syntax should be written literally; they are not metasyntax as used elsewhere in syntax descriptions.- SELECT left_tbl.*
You can use other types of joins within
{ OJ ... }, such asINNER JOINorRIGHT OUTER JOIN. This helps with compatibility with some third-party applications, but is not official ODBC syntax.STRAIGHT_JOINis similar toJOIN, except that the left table is always read before the right table. This can be used for those (few) cases for which the join optimizer processes the tables in a suboptimal order.
Some join examples:
Natural joins and joins with USING, including
outer join variants, are processed according to the SQL:2003
standard:
Redundant columns of a
NATURALjoin do not appear. Consider this set of statements:In the first
SELECTstatement, columnjappears in both tables and thus becomes a join column, so, according to standard SQL, it should appear only once in the output, not twice. Similarly, in the second SELECT statement, columnjis named in theUSINGclause and should appear only once in the output, not twice.Thus, the statements produce this output:
+------+------+------+ | j | i | k | +------+------+------+ | 1 | 1 | 1 | +------+------+------+ +------+------+------+ | j | i | k | +------+------+------+ | 1 | 1 | 1 | +------+------+------+Redundant column elimination and column ordering occurs according to standard SQL, producing this display order:
First, coalesced common columns of the two joined tables, in the order in which they occur in the first table
Second, columns unique to the first table, in order in which they occur in that table
Third, columns unique to the second table, in order in which they occur in that table
The single result column that replaces two common columns is defined using the coalesce operation. That is, for two
t1.aandt2.athe resulting single join columnais defined asa = COALESCE(t1.a, t2.a), where:If the join operation is any other join, the result columns of the join consist of the concatenation of all columns of the joined tables.
A consequence of the definition of coalesced columns is that, for outer joins, the coalesced column contains the value of the non-
NULLcolumn if one of the two columns is alwaysNULL. If neither or both columns areNULL, both common columns have the same value, so it doesn't matter which one is chosen as the value of the coalesced column. A simple way to interpret this is to consider that a coalesced column of an outer join is represented by the common column of the inner table of aJOIN. Suppose that the tablest1(a, b)andt2(a, c)have the following contents:t1 t2 ---- ---- 1 x 2 z 2 y 3 wThen, for this join, column
acontains the values oft1.a:- +------+------+------+
- | a | b | c |
- +------+------+------+
- +------+------+------+
By contrast, for this join, column
acontains the values oft2.a.- +------+------+------+
- | a | c | b |
- +------+------+------+
- +------+------+------+
Compare those results to the otherwise equivalent queries with
JOIN ... ON:- +------+------+------+------+
- | a | b | a | c |
- +------+------+------+------+
- +------+------+------+------+
A
USINGclause can be rewritten as anONclause that compares corresponding columns. However, althoughUSINGandONare similar, they are not quite the same. Consider the following two queries:With respect to determining which rows satisfy the join condition, both joins are semantically identical.
With respect to determining which columns to display for
SELECT *expansion, the two joins are not semantically identical. TheUSINGjoin selects the coalesced value of corresponding columns, whereas theONjoin selects all columns from all tables. For theUSINGjoin,SELECT *selects these values:For the
ONjoin,SELECT *selects these values:a.c1, a.c2, a.c3, b.c1, b.c2, b.c3With an inner join,
COALESCE(a.c1, b.c1)is the same as eithera.c1orb.c1because both columns will have the same value. With an outer join (such asLEFT JOIN), one of the two columns can beNULL. That column is omitted from the result.An
ONclause can refer only to its operands.Example:
The statement fails with an
Unknown column 'i3' in 'on clause'error becausei3is a column int3, which is not an operand of theONclause. To enable the join to be processed, rewrite the statement as follows:JOINhas higher precedence than the comma operator (,), so the join expressiont1, t2 JOIN t3is interpreted as(t1, (t2 JOIN t3)), not as((t1, t2) JOIN t3). This affects statements that use anONclause because that clause can refer only to columns in the operands of the join, and the precedence affects interpretation of what those operands are.Example:
The
JOINtakes precedence over the comma operator, so the operands for theONclause aret2andt3. Becauset1.i1is not a column in either of the operands, the result is anUnknown column 't1.i1' in 'on clause'error.To enable the join to be processed, use either of these strategies:
Group the first two tables explicitly with parentheses so that the operands for the
ONclause are(t1, t2)andt3:Avoid the use of the comma operator and use
JOINinstead:
The same precedence interpretation also applies to statements that mix the comma operator with
INNER JOIN,CROSS JOIN,LEFT JOIN, andRIGHT JOIN, all of which have higher precedence than the comma operator.A MySQL extension compared to the SQL:2003 standard is that MySQL permits you to qualify the common (coalesced) columns of
NATURALorUSINGjoins, whereas the standard disallows that.
Document created the 26/06/2006, last modified the 26/10/2018
Source of the printed document:https://www.gaudry.be/en//mysql-rf-join.html
The infobrol is a personal site whose content is my sole responsibility. The text is available under CreativeCommons license (BY-NC-SA). More info on the terms of use and the author.
References
These references and links indicate documents consulted during the writing of this page, or which may provide additional information, but the authors of these sources can not be held responsible for the content of this page.
The author of this site is solely responsible for the way in which the various concepts, and the freedoms that are taken with the reference works, are presented here. Remember that you must cross multiple source information to reduce the risk of errors.