There is a users table with uid , cash and points , etc.
There is another exchange_log table with the fields userid , summa , etc.
You need to create a trigger that tracks the transfer of the amount from cash to points and records the transfer amount and user id ( uid ) on the exchange_log table.
Here is what I tried but does not work:

 CREATE TRIGGER exchange_log AFTER UPDATE ON users FOR EACH ROW IF (NEW.cash != OLD.cash AND NEW.points != OLD.points AND (NEW.points-OLD.points) = (OLD.cash - NEW.CASH)) THEN INSERT INTO exchange_log(uid, summa) VALUES(NEW.uid,NEW.points-OLD.points); END IF 

Edit the trigger and show my error. Thank.

  • Did I specify the change tracking conditions only in the cash and points columns? Or do you need to specify other columns? For example: New.uid = OLD.uid - Shuhratjon Jumaev

1 answer 1

If in the exchange_log table the user's foreign key is called userid , it must be specified in the INSERT INTO instead of uid

 DELIMITER // CREATE TRIGGER exchange_log AFTER UPDATE ON users FOR EACH ROW IF (NEW.cash != OLD.cash AND NEW.points != OLD.points AND (NEW.points-OLD.points) = (OLD.cash - NEW.CASH)) THEN INSERT INTO exchange_log(userid, summa) VALUES(NEW.uid,NEW.points-OLD.points); END IF// 

After that, my trigger earned the way you describe it.

  • Did I specify the conditions correctly? Do I need to check that other columns do not change, If I want the trigger to work only when only two columns change (cash and points)? @cheops - Shuhratjon Jumaev
  • @ShuhratjonJumaev yes, the conditions are all right. Chased the trigger, everything is correct. - cheops