Eche un vistazo a INSERT... ON DUPLICATE KEY ACTUALIZAR .
Debería verse algo así:
INSERT INTO mytable (rating, num_rates, uniqueCol)
VALUES ($theRating, 1, $uniqueCol)
ON DUPLICATE KEY UPDATE
rating=((rating*num_rates)+$theRating)/num_rates,
num_rates=num_rates+1;
Asegúrate de tener un UNIQUE index
o PRIMARY KEY
en su uniqueCol
.