I have two MySQL tables: sms and consumption. The sms table contain all transactions while the consumption table contain the sum of sms consumed.
My goal is to be able to now the actual consumption for a day, so I need just to check directly on consumption table.
For it, I'm trying to create a trigger that will verify:
- if for day 1 there is no row in
consumptiontable, then insert a new row - if there is already a row for day 1, then update it
Here my code that doesn't work.
CREATE TRIGGER tg_new_sms_sent
AFTER INSERT ON sms
FOR EACH ROW
SET @k = (SELECT id FROM consumption WHERE period = NEW.simple_date);
CASE
WHEN ISNULL(@k) THEN
INSERT INTO consumption (system_id, period, credit, sms)
VALUES (NEW.system_id, NEW.simple_date, NEW.used_credit, NEW.sms_count);
ELSE
UPDATE consumption SET credit = credit + New.used_credit, sms = sms + NEW.sms_count WHERE (system_id = NEW.system_id) AND (period = NEW.simple_date);
END CASE
How can I do?