I've created a trigger that resembles the following:
delimiter //
CREATE TRIGGER update_total_seconds_on_phone AFTER INSERT
ON cdr
FOR EACH ROW
BEGIN
IF NEW.billsec > 0 AND NEW.userfield <> NULL THEN
UPDATE foo
SET total_seconds = total_seconds + NEW.billsec
WHERE phone_id = NEW.userfield;
END IF;
END;//
It seems to go through okay. However it doesn't appear to be invoking when I need it to. Here's an example:
mysql> select total_seconds from foo where phone_id = 1;
+---------------+
| total_seconds |
+---------------+
| 0 |
+---------------+
1 row in set (0.00 sec)
mysql> insert into cdr (billsec, userfield) VALUES(60, 1); Query OK, 1 row affected, 12 warnings (0.00 sec)
mysql> select total_seconds from foo where phone_id = 1;
+---------------+
| total_seconds |
+---------------+
| 0 |
+---------------+
EDIT: The warnings in this particular case do not affect the trigger. It's mostly additional columns which do not have default values in the cdr table that cause the warnings. For the sake of simplicity, I kept my INSERT statement brief.