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:

  1. table_references:
  2.     escaped_table_reference [, escaped_table_reference] ...
  3.  
  4. escaped_table_reference:
  5.     table_reference
  6.   | { OJ table_reference }
  7.  
  8. table_reference:
  9.     table_factor
  10.   | join_table
  11.  
  12. table_factor:
  13.     tbl_name [PARTITION (partition_names)]
  14.         [[AS] alias] [index_hint_list]
  15.   | table_subquery [AS] alias [(col_list)]
  16.   | ( table_references )
  17.  
  18. join_table:
  19.     table_reference [INNER | CROSS] JOIN table_factor [join_condition]
  20.   | table_reference STRAIGHT_JOIN table_factor
  21.   | table_reference STRAIGHT_JOIN table_factor ON conditional_expr
  22.   | table_reference {LEFT|RIGHT} [OUTER] JOIN table_reference join_condition
  23.   | table_reference NATURAL [INNER | {LEFT|RIGHT} [OUTER]] JOIN table_factor
  24.  
  25. join_condition:
  26.     ON conditional_expr
  27.   | USING (column_list)
  28.  
  29. index_hint_list:
  30.     index_hint [, index_hint] ...
  31.  
  32. index_hint:
  33.     USE {INDEX|KEY}
  34.       [FOR {JOIN|ORDER BY|GROUP BY}] ([index_list])
  35.       [FOR {JOIN|ORDER BY|GROUP BY}] (index_list)
  36.   | FORCE {INDEX|KEY}
  37.       [FOR {JOIN|ORDER BY|GROUP BY}] (index_list)
  38.  
  39. index_list:
  40.     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:

  1. SELECT * FROM t1 LEFT JOIN (t2, t3, t4)
  2.                  ON (t2.a = t1.a AND t3.b = t1.b AND t4.c = t1.c)

is equivalent to:

  1.                  ON (t2.a = t1.a AND t3.b = t1.b AND t4.c = t1.c)

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.2, “Optimizer Hints”, and Section 8.9.3, “Switchable Optimizations”.

The following list describes general factors to take into account when writing joins:

  • A table reference can be aliased using tbl_name AS alias_name or tbl_name alias_name:

    1. SELECT t1.name, t2.salary
    2.   FROM employee AS t1 INNER JOIN info AS t2 ON t1.name = t2.name;
    3.  
    4. SELECT t1.name, t2.salary
    5.   FROM employee t1 INNER JOIN info t2 ON t1.name = t2.name;
  • A table_subquery is also known as a derived table or subquery in the FROM clause. 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:

    1. SELECT * FROM (SELECT 1, 2, 3) AS t1;
  • INNER JOIN and , (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 form Unknown column 'col_name' in 'on clause' may occur. Information about dealing with this problem is given later in this section.

  • The conditional_expr used with ON is any conditional expression of the form that can be used in a WHERE clause. Generally, the ON clause serves for conditions that specify how to join tables, and the WHERE clause restricts which rows to include in the result set.

  • If there is no matching row for the right table in the ON or USING part in a LEFT JOIN, a row with all columns set to NULL is used for the right table. You can use this fact to find rows in a table that have no counterpart in another table:

    1. SELECT left_tbl.*
    2.   FROM left_tbl LEFT JOIN right_tbl ON left_tbl.id = right_tbl.id
    3.   WHERE right_tbl.id IS NULL;

    This example finds all rows in left_tbl with an id value that is not present in right_tbl (that is, all rows in left_tbl with no corresponding row in right_tbl). See Section 8.2.1.8, “Outer Join Optimization”.

  • The USING(column_list) clause names a list of columns that must exist in both tables. If tables a and b both contain columns c1, c2, and c3, the following join compares corresponding columns from the two tables:

    1. a LEFT JOIN b USING (c1, c2, c3)
  • The NATURAL [LEFT] JOIN of two tables is defined to be semantically equivalent to an INNER JOIN or a LEFT JOIN with a USING clause that names all columns that exist in both tables.

  • RIGHT JOIN works analogously to LEFT JOIN. To keep code portable across databases, it is recommended that you use LEFT JOIN instead of RIGHT 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.

    1. SELECT left_tbl.*
    2.     FROM { OJ left_tbl LEFT OUTER JOIN right_tbl ON left_tbl.id = right_tbl.id }
    3.     WHERE right_tbl.id IS NULL;

    You can use other types of joins within { OJ ... }, such as INNER JOIN or RIGHT OUTER JOIN. This helps with compatibility with some third-party applications, but is not official ODBC syntax.

  • STRAIGHT_JOIN is similar to JOIN, 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:

  1. SELECT * FROM table1, table2;
  2.  
  3. SELECT * FROM table1 INNER JOIN table2 ON table1.id = table2.id;
  4.  
  5. SELECT * FROM table1 LEFT JOIN table2 ON table1.id = table2.id;
  6.  
  7. SELECT * FROM table1 LEFT JOIN table2 USING (id);
  8.  
  9. SELECT * FROM table1 LEFT JOIN table2 ON table1.id = table2.id
  10.   LEFT JOIN table3 ON table2.id = table3.id;

Natural joins and joins with USING, including outer join variants, are processed according to the SQL:2003 standard:

  • Redundant columns of a NATURAL join do not appear. Consider this set of statements:

    1. CREATE TABLE t1 (i INT, j INT);
    2. CREATE TABLE t2 (k INT, j INT);
    3. INSERT INTO t1 VALUES(1, 1);
    4. INSERT INTO t2 VALUES(1, 1);
    5. SELECT * FROM t1 JOIN t2 USING (j);

    In the first SELECT statement, column j appears 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, column j is named in the USING clause 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.a and t2.a the resulting single join column a is defined as a = 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-NULL column if one of the two columns is always NULL. If neither or both columns are NULL, 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 a JOIN. Suppose that the tables t1(a, b) and t2(a, c) have the following contents:

    t1    t2
    ----  ----
    1 x   2 z
    2 y   3 w

    Then, for this join, column a contains the values of t1.a:

    1. mysql> SELECT * FROM t1 NATURAL LEFT JOIN t2;
    2. +------+------+------+
    3. | a    | b    | c    |
    4. +------+------+------+
    5. |    1 | x    | NULL |
    6. |    2 | y    | z    |
    7. +------+------+------+

    By contrast, for this join, column a contains the values of t2.a.

    1. mysql> SELECT * FROM t1 NATURAL RIGHT JOIN t2;
    2. +------+------+------+
    3. | a    | c    | b    |
    4. +------+------+------+
    5. |    2 | z    | y    |
    6. |    3 | w    | NULL |
    7. +------+------+------+

    Compare those results to the otherwise equivalent queries with JOIN ... ON:

    1. mysql> SELECT * FROM t1 LEFT JOIN t2 ON (t1.a = t2.a);
    2. +------+------+------+------+
    3. | a    | b    | a    | c    |
    4. +------+------+------+------+
    5. |    1 | x    | NULL | NULL |
    6. |    2 | y    |    2 | z    |
    7. +------+------+------+------+
    1. mysql> SELECT * FROM t1 RIGHT JOIN t2 ON (t1.a = t2.a);
    2. +------+------+------+------+
    3. | a    | b    | a    | c    |
    4. +------+------+------+------+
    5. |    2 | y    |    2 | z    |
    6. | NULL | NULL |    3 | w    |
    7. +------+------+------+------+
  • A USING clause can be rewritten as an ON clause that compares corresponding columns. However, although USING and ON are similar, they are not quite the same. Consider the following two queries:

    1. a LEFT JOIN b USING (c1, c2, c3)
    2. a LEFT JOIN b ON a.c1 = b.c1 AND a.c2 = b.c2 AND a.c3 = b.c3

    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. The USING join selects the coalesced value of corresponding columns, whereas the ON join selects all columns from all tables. For the USING join, SELECT * selects these values:

    1. COALESCE(a.c1, b.c1), COALESCE(a.c2, b.c2), COALESCE(a.c3, b.c3)

    For the ON join, SELECT * selects these values:

    a.c1, a.c2, a.c3, b.c1, b.c2, b.c3

    With an inner join, COALESCE(a.c1, b.c1) is the same as either a.c1 or b.c1 because both columns will have the same value. With an outer join (such as LEFT JOIN), one of the two columns can be NULL. That column is omitted from the result.

  • An ON clause can refer only to its operands.

    Example:

    1. CREATE TABLE t1 (i1 INT);
    2. CREATE TABLE t2 (i2 INT);
    3. CREATE TABLE t3 (i3 INT);
    4. SELECT * FROM t1 JOIN t2 ON (i1 = i3) JOIN t3;

    The statement fails with an Unknown column 'i3' in 'on clause' error because i3 is a column in t3, which is not an operand of the ON clause. To enable the join to be processed, rewrite the statement as follows:

    1. SELECT * FROM t1 JOIN t2 JOIN t3 ON (i1 = i3);
  • JOIN has higher precedence than the comma operator (,), so the join expression t1, t2 JOIN t3 is interpreted as (t1, (t2 JOIN t3)), not as ((t1, t2) JOIN t3). This affects statements that use an ON clause 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:

    1. CREATE TABLE t1 (i1 INT, j1 INT);
    2. CREATE TABLE t2 (i2 INT, j2 INT);
    3. CREATE TABLE t3 (i3 INT, j3 INT);
    4. INSERT INTO t1 VALUES(1, 1);
    5. INSERT INTO t2 VALUES(1, 1);
    6. INSERT INTO t3 VALUES(1, 1);
    7. SELECT * FROM t1, t2 JOIN t3 ON (t1.i1 = t3.i3);

    The JOIN takes precedence over the comma operator, so the operands for the ON clause are t2 and t3. Because t1.i1 is not a column in either of the operands, the result is an Unknown 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 ON clause are (t1, t2) and t3:

      1. SELECT * FROM (t1, t2) JOIN t3 ON (t1.i1 = t3.i3);
    • Avoid the use of the comma operator and use JOIN instead:

      1. SELECT * FROM t1 JOIN t2 JOIN t3 ON (t1.i1 = t3.i3);

    The same precedence interpretation also applies to statements that mix the comma operator with INNER JOIN, CROSS JOIN, LEFT JOIN, and RIGHT 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 NATURAL or USING joins, whereas the standard disallows that.


Rechercher dans le manuel MySQL

Traduction non disponible

Le manuel MySQL n'est pas encore traduit en français sur l'infobrol. Seule la version anglaise est disponible pour l'instant.

Document créé le 26/06/2006, dernière modification le 26/10/2018
Source du document imprimé : https://www.gaudry.be/mysql-rf-join.html

L'infobrol est un site personnel dont le contenu n'engage que moi. Le texte est mis à disposition sous licence CreativeCommons(BY-NC-SA). Plus d'info sur les conditions d'utilisation et sur l'auteur.

Références

  1. Consulter le document html Langue du document :en Manuel MySQL : https://dev.mysql.com/

Ces références et liens indiquent des documents consultés lors de la rédaction de cette page, ou qui peuvent apporter un complément d'information, mais les auteurs de ces sources ne peuvent être tenus responsables du contenu de cette page.
L'auteur de ce site est seul responsable de la manière dont sont présentés ici les différents concepts, et des libertés qui sont prises avec les ouvrages de référence. N'oubliez pas que vous devez croiser les informations de sources multiples afin de diminuer les risques d'erreurs.

Table des matières Haut