I have the following table structure:
And this trigger:
DELIMITER |
CREATE TRIGGER `credits__after_insert` AFTER INSERT ON `credits`
FOR EACH ROW BEGIN
UPDATE `clients`
SET `clients`.`available_credits` =
(SELECT SUM(`amount`) FROM `credits`
WHERE `client_id` = NEW.client_id )
WHERE `clients`.`id` = NEW.client_id;
UPDATE `credits`
SET NEW.`remaining` =
(SELECT SUM(`amount`) FROM `credits`
WHERE `client_id` = NEW.client_id );
END;
|
DELIMITER ;
Every time I try to insert a new row I receive the following error:
#1442 - Can't update table 'credits' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.
I don't understand why it gives me this error. I also don't want to receive similar questions/answers because I've tried everything from SO in past two hours. I want a simple to comprehend explanation, and if possible a solution.
Thanks.