简体   繁体   中英

mySQL UPDATE all columns from corresponding columns in another table

I have two tables with identical schema. And lots of columns!

I can update a record from the corresponding table by doing

update t1 
join t2 on t2.id=t1.id 
set t1.column1=t2.column1,
    t1.column2=t2.column2... 
where t2.columnx > 123;

But I have a ton of fields and am by nature, a lazy bastard who'd rather shave a yak and post on SE than type out a list of columns (possibly missing 1).

Other than some funky solution that writes out the column list to a text file etc, is there valid mySQL syntax that skips the explicit listing of all the columns and works more like INSERT...SELECT?

REPLACE INTO t1 SELECT * FROM t2 WHERE columnx>123;

Try to use REPLACE INTO ... SELECT ... syntax http://dev.mysql.com/doc/refman/5.0/en/replace.html

REPLACE INTO t1 SELECT t2.* FROM t2, t1 WHERE t1.columnx>123 AND t1.id=t2.id;

Warning! Not tested!

This code would work only if tables have unique indexes on id columns.

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