INSERT [LOW_PRIORITY | HIGH_PRIORITY] [IGNORE] [INTO] tbl_name [(col_name,...)] SELECT ... [ ON DUPLICATE KEY UPDATE col_name=expr, ... ]
With INSERT ... SELECT
, you can quickly insert many rows into a table from one or more other 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;
tbl_name
can also be specified in the form db_name
.tbl_name
(see Identifier Qualifiers). This allows to copy rows between different databases.
If the new table has a primary key or UNIQUE indexes, you can use IGNORE to handle duplicate key errors during the query. The newer values will not be inserted if an identical value already exists.
REPLACE
can be used instead of INSERT
to prevent duplicates on UNIQUE
indexes by deleting old values. In that case, ON DUPLICATE KEY UPDATE
cannot be used.
INSERT ... SELECT
works for tables which already exist. To create a table for a given resultset, you can use CREATE TABLE ... SELECT.
© 2019 MariaDB
Licensed under the Creative Commons Attribution 3.0 Unported License and the GNU Free Documentation License.
https://mariadb.com/kb/en/insert-select/