简体   繁体   中英

Insert rows into mySQL table where target table has more columns

I have 2 tables. I want to replace rows in table 1 with the rows in table2. However table 1 has more columns (appended after the common column names) so the following will not work...

replace into table1 select * from table2;

So I have to list all the column names ...

replace into table1 (col1, col2, ...) select col1, col2, ... from table2;

Is there a shortcut way to do something like this without actually listing all the columns?

Or is there a way to generate the column names from the table so I can go? ...

replace into table1 <list of columns that are in table2> 
    select <list of columns that are in table2> from table2;
INSERT INTO table1 (a,b,c,d)
SELECT field_that_becomes_a, null as becomes_b, field_that_becomes_c, 'static value' as becomes_d
FROM table2

Any fields you don't specify in table1 will simply assume their specified default values (or kill the query if there is no default).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM