Given the following table structures:
T1:
id a
1 1
2 2
3 3
T2:
id t1_id b
1 1 1
2 1 2
3 2 3
I need to add the value of t2.b to the value of t1.a where t2.t1_id = t1.id.
A simple update with a join like the following isn't working:
UPDATE t1
JOIN t2
ON t2.t1_id = t1.id
SET t1.a = t1.a + t2.b
WHERE t2.id IN(1,2)
Expected T1 result (adds 1 and 2 to t1.a = 1; 3 to t1.a = 2):
id a
1 4
2 5
3 3
Actual T1 result (only adds 1 to t1.a = 1; 3 to t1.a = 2):
id a
1 2
2 5
3 3
At the moment I'm looking at a select that computes the full sum of values to be added using a group by, and then that result set is joined to the update... This seems like overkill for something so simple though! Does anyone have an elegant and efficient solution?