I have three tables:
TableA
+-------+--------+--------+
| id_a | name | total |
+-------+--------+--------+
| 1 | Andrew | |
| 2 | Jhon | |
+-------+--------+--------+
TableB
+-------+--------+--------+--------+
| id_b | id_a | amount | id_c |
+-------+--------+--------+--------+
| 1 | 1 | 5 | 1 |
| 2 | 1 | 1 | 2 |
+-------+--------+--------+--------+
TableC
+-------+--------+
| id_c | status |
+-------+--------+
| 1 | 1 |
| 2 | 0 |
+-------+--------+
So what I need to do is to count a total sum of amounts from TableB where id_a = (1 or my posted id) AND where status of id_c is 1 or 0 and set it into the TableA in total column, for the data in the tables I’ve posted above the field total in a TableA where id_a = 1 will contains a 6 value.
I’m trying to go in my query through this way:
UPDATE TableA SET total=(SELECT SUM(amount) as sum FROM TableB WHERE id_a = 1 GROUP BY(sum))
but this is way is wrong ( I think ). How would be nice query to this?
You can use an
INNER JOINbetween TableB and TableC in your innerSELECT.