Mysql – Insert data from a source table into a destination table with more columns

insertMySQL

I want to use INSERT INTO SELECT * to insert data into a dest table from a source table, but I have some extra columns added to destination table. These columns are distributed around the table like 2 are at top and 2 are at bottom.

Also for insertion I can insert some default value like null to these columns.

Best Answer

SELECT * selects col1, col2, col3, as defined in table2, and if the selection doesn't match the target, it will fail.

the conclusion is to alter the selection:

INSERT INTO table1 
SELECT col1, col2, NULL as col_extra, 'default' as col_extra2, col3, [...] 
FROM table2

not tested, but should work