简体   繁体   English

mySQL UPDATE来自另一个表中相应列的所有列

[英]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). 但是我有很多田地,而且本质上是一个懒惰的混蛋,他宁愿刮牦牛并在SE上发帖,也不会列出一列列(可能缺少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? 除了将列列表写入文本文件等的一些时髦的解决方案之外,是否有有效的mySQL语法跳过所有列的显式列表并且更像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 ... SELECT ...语法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. 仅当表在id列上具有唯一索引时,此代码才有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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