top of page
CerebroSQL

MySQL: 

INSERT SELECT

Syntax:
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. 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:

INSERT INTO ta TABLE tb;

TABLE tb is equivalent to 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. 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. For more
information, see [HELP TABLE].

URL: https://dev.mysql.com/doc/refman/8.0/en/insert-select.html

Example

bottom of page