I have a table like this:
A
ID_A ID_1 ID_2
1 1 (null)
2 3 (null)
3 7 (null)
B
ID_B ID_1 ID_2
1 (null) 2
2 (null) 4
3 (null) 6
REF
ID_A ID_B
1 2
3 1
According to the ref table, the object with an ID_A of 2 is the same object as an ID_B of 1.
Therefore, I should be able to update the table this way:
A
ID_A ID_1 ID_2
1 1 4
2 3 (null)
3 7 2
Indeed, you get that result if you do this query:
select
A.ID_A, B.ID_1, C.ID_2
from
A, B, REF
where
A.ID_A = REF.ID_A
AND REF.ID_B = B.ID_B
(actually you lose the null row because it’s an inner join, but that’s not the point.)
What I am completely unable to do is update A with this new information! I either get “single-row subquery returns more than one row” with an update or the lovely result that my query is non-deterministic with a merge.
Given that I really do have the three tables as I’ve shown, how can I write a query to update id_2 correctly?
;
SQL Fiddle