0

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!

user1637359
  • 227
  • 4
  • 10

1 Answers1

0

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.

Andrew Lazarus
  • 18,205
  • 3
  • 35
  • 53