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 从MySQL 8.0.19开始,您可以使用TABLE statement in place of SELECT, as shown here:TABLE语句代替SELECT,如下所示:
INSERT INTO ta TABLE tb;
TABLE tb is equivalent to SELECT * FROM tb. TABLE tb等同于SELECT * FROM tb。It 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也适用:
Specify 指定IGNORE to ignore rows that would cause duplicate-key violations.IGNORE忽略会导致重复键冲突的行。
The target table of the INSERT statement may appear in the FROM clause of the SELECT part of the query, or as the table named by TABLE. INSERT语句的目标表可能出现在查询的SELECT部分的FROM子句中,也可能出现在以TABLE命名的表中。However, you cannot insert into a table and select from the same table in a subquery.但是,不能插入到表中,也不能从子查询中的同一个表中进行选择。
When selecting from and inserting into the same table, MySQL creates an internal temporary table to hold the rows from the 当从同一个表中选择并插入时,MySQL会创建一个内部临时表来保存SELECT and then inserts those rows into the target table. SELECT中的行,然后将这些行插入到目标表中。However, you cannot use 但是,当INSERT INTO t ... SELECT ... FROM t when t is a TEMPORARY table, because TEMPORARY tables cannot be referred to twice in the same statement. t是临时表时不能使用INSERT INTO t ... SELECT ... FROM t,因为临时表不能在同一语句中引用两次。For the same reason, you cannot use 出于同样的原因,当INSERT INTO t ... TABLE t when t is a temporary table. t是临时表时,你不能使用INSERT INTO t ... TABLE t。See Section 8.4.4, “Internal Temporary Table Use in MySQL”, and Section B.3.6.2, “TEMPORARY Table Problems”.请参阅第8.4.4节,“MySQL中的内部临时表使用”和第B.3.6.2节,“临时表问题”。
AUTO_INCREMENT columns work as usual.AUTO_INCREMENT列正常工作。
To ensure that the binary log can be used to re-create the original tables, MySQL does not permit concurrent inserts for 为了确保二进制日志可以用来重新创建原始表,MySQL不允许对INSERT ... SELECT or INSERT ... TABLE statements (see Section 8.11.3, “Concurrent Inserts”).INSERT ... SELECT语句或INSERT ... TABLE语句进行并发插入(请参阅第8.11.3节,“并发插入”)。
To avoid ambiguous column reference problems when the 为避免SELECT and the INSERT refer to the same table, provide a unique alias for each table used in the SELECT part, and qualify column names in that part with the appropriate alias.SELECT和INSERT引用同一个表时出现不明确的列引用问题,请为SELECT部件中使用的每个表提供唯一的别名,并使用适当的别名限定该部件中的列名。
The TABLE statement does not support aliases.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子句的SELECT或TABLE语句返回行的顺序是不确定的。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 ... SELECT或INSERT ... 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 UPDATE和INSERT 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 当使用基于语句的模式时,此类语句在错误日志中生成警告,当使用混合模式时,将使用基于行的格式写入二进制日志。(Bug #11758262, Bug #50439)MIXED mode.
See also Section 17.2.1.1, “Advantages and Disadvantages of Statement-Based and Row-Based Replication”.另请参阅第17.2.1.1节,“基于语句和基于行的复制的优缺点”。