UPDATE Same Row After UPDATE in Trigger

后端 未结 2 1393
轻奢々
轻奢々 2020-12-14 04:18

I want the epc column to always be earnings/clicks. I am using an AFTER UPDATE trigger to accomplish this. So if I were to add 100 cli

2条回答
  •  情歌与酒
    2020-12-14 04:44

    You can't update rows in the table in an after update trigger.

    Perhaps you want something like this:

    CREATE TRIGGER `records_integrity` BEFORE UPDATE
    ON `records`
    FOR EACH ROW
        SET NEW.epc=IFNULL(new.earnings/new.clicks, 0);
    

    EDIT:

    Inside a trigger, you have have access to OLD and NEW. OLD are the old values in the record and NEW are the new values. In a before trigger, the NEW values are what get written to the table, so you can modify them. In an after trigger, the NEW values have already been written, so they cannot be modified. I think the MySQL documentation explains this pretty well.

提交回复
热议问题