13.2.6.1 INSERT ... SELECT Statement语句

INSERT [LOW_PRIORITY | HIGH_PRIORITY] [IGNORE]
    [INTO] tbl_name
    [PARTITION (partition_name [, partition_name] ...)]
    [(col_name [, col_name] ...)]
    {SELECT ... | TABLE table_name}
    [ON DUPLICATE KEY UPDATE assignment_list]
value:
    {expr | DEFAULT}
assignment:
col_name = value
assignment_list:
assignment [, assignment] ...

With INSERT ... SELECT, you can quickly insert many rows into a table from the result of a SELECT statement, which can select from one or many tables. 使用INSERT ... SELECT,您可以从SELECT语句的结果中快速地将许多行插入表中,SELECT语句可以从一个或多个表中进行选择。For example:例如:

INSERT INTO tbl_temp2 (fld_id)
  SELECT tbl_temp1.fld_order_id
  FROM tbl_temp1 WHERE tbl_temp1.fld_order_id > 100;

Beginning with MySQL 8.0.19, you can use a TABLE statement in place of SELECT, as shown here:从MySQL 8.0.19开始,您可以使用TABLE语句代替SELECT,如下所示:

INSERT INTO ta TABLE tb;

TABLE tb is equivalent to SELECT * FROM tb. TABLE tb等同于SELECT * FROM tbIt can be useful when inserting all columns from the source table into the target table, and no filtering with WHERE is required. 当将源表中的所有列插入到目标表中时,它非常有用,并且不需要使用WHERE进行过滤。In addition, the rows from TABLE can be ordered by one or more columns using ORDER BY, and the number of rows inserted can be limited using a LIMIT clause. 此外,TABLE中的行可以使用ORDER BY按一列或多列排序,插入的行数可以使用LIMIT子句限制。For more information, see Section 13.2.12, “TABLE Statement”.有关更多信息,请参阅第13.2.12节,“表语句”

The following conditions hold for INSERT ... SELECT statements, and, except where noted, for INSERT ... TABLE as well:以下条件适用于INSERT ... SELECT语句,并且,除非另有说明,否则INSERT ... TABLE也适用:

You can explicitly select which partitions or subpartitions (or both) of the source or target table (or both) are to be used with a PARTITION clause following the name of the table. 您可以显式地选择源表或目标表(或两者)的哪些分区或子分区(或两者)与表名后面的PARTITION子句一起使用。When PARTITION is used with the name of the source table in the SELECT portion of the statement, rows are selected only from the partitions or subpartitions named in its partition list. PARTITION与语句的SELECT部分中的源表的名称一起使用时,只从分区列表中指定的分区或子分区中选择行。When PARTITION is used with the name of the target table for the INSERT portion of the statement, it must be possible to insert all rows selected into the partitions or subpartitions named in the partition list following the option. PARTITION与语句的INSERT部分的目标表的名称一起使用时,必须能够将所有选中的行插入到选项后面的分区列表中命名的分区或子分区中。Otherwise, the INSERT ... SELECT statement fails. 否则,INSERT ... SELECT语句失败。For more information and examples, see Section 24.5, “Partition Selection”.有关更多信息和示例,请参阅第24.5节,“分区选择”

TABLE does not support a PARTITION clause.TABLE不支持PARTITION子句。

For INSERT ... SELECT statements, see Section 13.2.6.2, “INSERT ... ON DUPLICATE KEY UPDATE Statement” for conditions under which the SELECT columns can be referred to in an ON DUPLICATE KEY UPDATE clause. 对于INSERT ... SELECT语句,请参阅第13.2.6.2节“INSERT ... ON DUPLICATE KEY UPDATE语句”,它讲了在ON DUPLICATE KEY UPDATE子句中可以引用SELECT列的条件。This also works for INSERT ... TABLE.这也适用于INSERT ... TABLE

The order in which a SELECT or TABLE statement with no ORDER BY clause returns rows is nondeterministic. 没有ORDER BY子句的SELECTTABLE语句返回行的顺序是不确定的。This means that, when using replication, there is no guarantee that such a SELECT returns rows in the same order on the source and the replica, which can lead to inconsistencies between them. 这意味着,在使用复制时,不能保证这样的SELECT在源和复制副本上以相同的顺序返回行,这可能导致它们之间的不一致。To prevent this from occurring, always write INSERT ... SELECT or INSERT ... TABLE statements that are to be replicated using an ORDER BY clause that produces the same row order on the source and the replica. 要防止发生这种情况,在写INSERT ... SELECTINSERT ... TABLE语句时请始终使用ORDER BY子句使其可复现,该子句在源和副本上生成相同的行顺序。See also Section 17.5.1.18, “Replication and LIMIT”.另见第17.5.1.18节,“复制和限制”

Due to this issue, INSERT ... SELECT ON DUPLICATE KEY UPDATE and INSERT IGNORE ... SELECT statements are flagged as unsafe for statement-based replication. 由于其基于语句的复现性问题,INSERT ... SELECT ON DUPLICATE KEY UPDATEINSERT IGNORE ... SELECT语句被标记为不安全。Such statements produce a warning in the error log when using statement-based mode and are written to the binary log using the row-based format when using MIXED mode. 当使用基于语句的模式时,此类语句在错误日志中生成警告,当使用混合模式时,将使用基于行的格式写入二进制日志。(Bug #11758262, Bug #50439)

See also Section 17.2.1.1, “Advantages and Disadvantages of Statement-Based and Row-Based Replication”.另请参阅第17.2.1.1节,“基于语句和基于行的复制的优缺点”