13.2.10.2 JOIN ClauseJOIN子句

MySQL supports the following JOIN syntax for the table_references part of SELECT statements and multiple-table DELETE and UPDATE statements:MySQL支持SELECT语句和多表DELETEUPDATE语句的table_reference部分的JOIN语法如下:

table_references:
escaped_table_reference [, escaped_table_reference] ...
escaped_table_reference: {
table_reference
  | { OJ table_reference }
}
table_reference: {
table_factor
  | joined_table
}
table_factor: {
tbl_name [PARTITION (partition_names)]
        [[AS] alias] [index_hint_list]
  | [LATERAL] table_subquery [AS] alias [(col_list)]
  | ( table_references )
}
joined_table: {
table_reference {[INNER | CROSS] JOIN | STRAIGHT_JOIN} table_factor [join_specification]
  | table_reference {LEFT|RIGHT} [OUTER] JOIN table_reference join_specification
  | table_reference NATURAL [INNER | {LEFT|RIGHT} [OUTER]] JOIN table_factor
}
join_specification: {
    ON search_condition
  | USING (join_column_list)
}
join_column_list:
column_name [, column_name] ...
index_hint_list:
index_hint [, index_hint] ...
index_hint: {
    USE {INDEX|KEY}
      [FOR {JOIN|ORDER BY|GROUP BY}] ([index_list])
  | {IGNORE|FORCE} {INDEX|KEY}
      [FOR {JOIN|ORDER BY|GROUP BY}] (index_list)
}
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 clause, including a list of comma-separated partitions, subpartitions, or both. 一个表引用(当它引用一个分区表时)可以包含一个PARTITION子句,包括一个逗号分隔的分区、子分区或两者的列表。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 24.5, “Partition Selection”.有关更多信息和示例,请参阅第24.5节,“分区选择”

The syntax of table_factor is extended in MySQL in comparison with standard SQL. 与标准SQL相比,MySQL扩展了table_factor的语法。The standard accepts only table_reference, not a list of them inside a pair of parentheses.标准只接受table_reference,而不接受一对括号内的表的列表。

This is a conservative extension if each comma in a list of table_reference items is considered as equivalent to an inner join. 如果table_reference项列表中的每个逗号都被视为等同于内部联接,则这是一个保守的扩展。For example:例如:

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

is equivalent to:

SELECT * FROM t1 LEFT JOIN (t2 CROSS JOIN t3 CROSS JOIN t4)
                 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). 在MySQL中,JOINCROSS JOININNER JOIN是语法等价物(它们可以互相替换)。In standard SQL, they are not equivalent. 在标准SQL中,它们不是等价的。INNER JOIN is used with an ON clause, CROSS JOIN is used otherwise.INNER JOINON子句一起使用,否则使用CROSS JOIN

In general, parentheses can be ignored in join expressions containing only inner join operations. 通常,在只包含内部联接操作的联接表达式中可以忽略括号。MySQL also supports nested joins. MySQL还支持嵌套连接。See Section 8.2.1.8, “Nested Join Optimization”.请参阅第8.2.1.8节,“嵌套连接优化”

Index hints can be specified to affect how the MySQL optimizer makes use of indexes. 可以指定索引提示来影响MySQL优化器如何使用索引。For more information, see Section 8.9.4, “Index Hints”. 有关更多信息,请参阅第8.9.4节,“索引提示”Optimizer hints and the optimizer_switch system variable are other ways to influence optimizer use of indexes. 优化器提示和optimizer_switch系统变量是影响优化器使用索引的其他方法。See Section 8.9.3, “Optimizer Hints”, and Section 8.9.2, “Switchable Optimizations”.请参阅第8.9.3节,“优化器提示”第8.9.2节,“可切换优化”

The following list describes general factors to take into account when writing joins:下表描述了写入联接时要考虑的一般因素:

Some join examples:一些连接示例:

SELECT * FROM table1, table2;

SELECT * FROM table1 INNER JOIN table2 ON table1.id = table2.id;

SELECT * FROM table1 LEFT JOIN table2 ON table1.id = table2.id;

SELECT * FROM table1 LEFT JOIN table2 USING (id);

SELECT * FROM table1 LEFT JOIN table2 ON table1.id = table2.id
  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:自然连接和使用USING的连接(包括外部连接变体)根据SQL:2003标准进行处理: