简体   繁体   中英

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   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 ). Looks like the following is required. Except, I don't have MySQL to test on.

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 ; we don't want a reserved word there.

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