INSERT [LOW_PRIORITY | HIGH_PRIORITY] [IGNORE] [INTO]
col_name,...)] SELECT ... [ ON DUPLICATE KEY UPDATE
expr, ... ]
SELECT, you can quickly insert many rows into a table
from one or many tables. For example:
INSERT INTO tbl_temp2 (fld_id) SELECT tbl_temp1.fld_order_id FROM tbl_temp1 WHERE tbl_temp1.fld_order_id > 100;
The following conditions hold for a
IGNORE to ignore rows that would
cause duplicate-key violations.
DELAYED is ignored with
The target table of the
INSERT statement may appear
FROM clause of the
SELECT part of the query.
(This was not possible in some older versions of MySQL.)
However, you cannot insert into a table and select from the
same table in a subquery.
When selecting from and inserting into a table at the same
time, MySQL creates a temporary table to hold the rows from
SELECT and then inserts
those rows into the target table. However, it remains true
that 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 (see
Section C.5.7.2, “
TEMPORARY Table Problems”).
AUTO_INCREMENT columns work as usual.
To ensure that the binary log can be used to re-create the
original tables, MySQL does not permit concurrent inserts
... SELECT statements.
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.
In the values part of
ON DUPLICATE KEY
UPDATE, you can refer to columns in other tables, as
long as you do not use
GROUP BY in the
SELECT part. One side effect is
that you must qualify nonunique column names in the values part.
The order in which rows are returned by a
SELECT statement with no
ORDER BY clause is not determined. This means
that, when using replication, there is no guarantee that such a
SELECT returns rows in the same order on the
master and the slave; this can lead to inconsistencies between
them. To prevent this from occurring, you should always write
INSERT ... SELECT statements that are to be
INSERT ... SELECT ... ORDER BY
. The choice of
column does not matter as long as the
same order for returning the rows is enforced on both the master
and the slave. See also
Section 22.214.171.124, “Replication and
Due to this issue, beginning with MySQL 5.5.18,
SELECT ON DUPLICATE KEY UPDATE and
INSERT IGNORE ...
SELECT statements are flagged as unsafe for
statement-based replication. With this change, such statements
produce a warning in the log when using statement-based mode and
are logged using the row-based format when using
MIXED mode. (Bug #11758262, Bug #50439)
INSERT ... SELECT statement that acts on
partitioned tables using a storage engine such as
MyISAM that employs table-level
locks locks all partitions of the source and target tables. This
does not occur with tables using storage engines such as
InnoDB that employ row-level
locking. This issue is resolved in MySQL 5.6. See
Section 18.5.4, “Partitioning and Table-Level Locking”, for more
Copyright © 1997, 2014, Oracle and/or its affiliates. All rights reserved. Legal Notices