Wednesday, 21 August 2013

mysql cross-updating a table

mysql cross-updating a table

I can't figure out how to solve this problem. I have a table (A) I need to
update, with a structure like this:
CODE VALUE
1 a
2 null
3 null
etc...
Then I have another table (B) with the same structure but with every value
set:
CODE VALUE
1 a
2 b
3 c
What I need to do is to copy data from table B to table A where A.CODE =
B.CODE but only if A.VALUE is not set. What's the best query to do so?
Can't do it by hand since I'm dealing with 2000ish rows.
I wrote something like this but it doesn't seem to work:
update A set VALUE =
(select b.VALUEfrom B b, A a where b.CODE = a.CODE)
Thanks in advance!

No comments:

Post a Comment