All pages
Powered by GitBook
1 of 1

Loading...

INSERT SELECT

Copy data between tables. This statement inserts the result set of a SELECT query directly into a target table, enabling efficient bulk data transfer.

Syntax

Description

With INSERT ... SELECT, you can quickly insert many rows into a table from one or more other tables. For example:

tbl_name can also be specified in the form db_name.tbl_name (see ). This allows to copy rows between different databases.

If the new table has a primary key or UNIQUE indexes, you can use to handle duplicate key errors during the query. The newer values will not be inserted if an identical value already exists.

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 .

See Also

This page is licensed: GPLv2, originally from

INSERT [LOW_PRIORITY | HIGH_PRIORITY] [IGNORE]
    [INTO] tbl_name [(col_name,...)]
    SELECT ...
    [ ON DUPLICATE KEY UPDATE col_name=expr, ... ]
INSERT - Default & Duplicate Values
  • INSERT IGNORE

  • INSERT ON DUPLICATE KEY UPDATE

  • Identifier Qualifiers
    IGNORE
    REPLACE
    CREATE TABLE ... SELECT
    INSERT
    INSERT DELAYED
    HIGH_PRIORITY and LOW_PRIORITY
    Concurrent Inserts
    fill_help_tables.sql
    INSERT INTO tbl_temp2 (fld_id)
      SELECT tbl_temp1.fld_order_id
      FROM tbl_temp1 WHERE tbl_temp1.fld_order_id > 100;