I think that the best and faster way to do this is to apply a UNIQUE
Constraint on A + B.
Alter TABLE TableB ADD CONSTRAINT unique_A_B UNIQUE(A,B);
Then use ON DUPLICATE KEY UPDATE
:
INSERT INTO TableB (A,B,C)
SELECT
SUM(A),
SUM(B),
SUM(C)
FROM TableA
GROUP BY A, B
ON DUPLICATE KEY UPDATE C=SUM(C);
For the example purpose I updated C
but I guess that in a real situation you'd want to update the Update Timestamp of your row with Now()
.