This should be a simple problem but I can’t seem to find an exact solution. This is basically what I have:
table_1:
id* scenario* val1 val2
--- -------- ----- ------
AC1 1 2 0
AC2 1 1 0
AC1 2 4 0
AC2 2 3 0
table_2:
id* val3
--- ------
AC1 4
AC2 6
All I want to do is update val2 of table_1 where scenario = 1 with values from table_2 with matching id but I want it to equal val1 + val3 (I don’t know how to make this less obfuscated). So for this example I want table_1 to become:
id* scenario* val1 val2
--- -------- ------ --------
AC1 1 2 6
AC2 1 1 7
AC1 2 4 0
AC2 2 3 0
I think this can be done with a simple MySQL statement but I don’t know how to structure it with a condition for the first table (“INSERT INTO table_1 WHERE scenario=1 SELECT…”??) and the arithmetic of two columns from two different tables.
Try this: