简体   繁体   English

如何将一行中的条目复制到另一行,但在mysql的同一列中

[英]how to copy an entry in a row to another row, but in the same column in mysql

this might sound like a trivial question but i've looked everywhere. 这听起来像是一个琐碎的问题,但我到处都是。 I have a table like this: 我有一张这样的桌子:

id   var1
1
2    19353

there is no entry for var1 where id =1 and I want to copy the entry for var1 from the other row to that location so that i have 没有id为= 1的var1的条目,我想将var1的条目从另一行复制到该位置,以便

id   var1
1    19353
2    19353

i've tried but was unsuccessful with: 我已经尝试过但未成功:

update table set var1 = (select var1 from table where id=2) where id=1;

any advice? 有什么建议吗? thanks! 谢谢!

MySQL seems to have its own ideas about the syntax for multi-table update (a/k/a UPDATE FROM ). MySQL似乎对多表更新(a / k / a UPDATE FROM )的语法有自己的想法。 Looks like the following is required. 看起来以下是必需的。 Except, I don't have MySQL to test on. 除了,我没有MySQL可以测试。

UPDATE t AS t1, t AS t2
SET t1.var1 = t2.var1
WHERE t1.id=1 AND t2.id=2;

EDIT: I have changed table name to t ; 编辑:我已将表名称更改为t we don't want a reserved word there. 我们不想在那里保留字。

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

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