MYSQL Trigger update on a table

mariadbMySQLphpmyadmin

Assuming that I have a table, user. How do I calculate the sum of votes and paymentAmt each time they are being updated using trigger? Then, the votes and paymentAmt will be added to the point. I want to do it for every users (1 and 2) everytime the they are updated without selecting the specific user id.May i know how do i automatically update points for each user? My trigger does not update the table even after I alter the datas

1) user_ID | votes | paymentAmt | point (USER)
    1          10        20        30 <-- automatically updated
    2          25        30        55
    3          10        40        50

Trigger

CREATE Trigger upd_pt 
AFTER UPDATE ON user
FOR EACH ROW 
BEGIN
    UPDATE user 
        set point = (select sum(votes) from user) + user.paymentAmt 
        where user.user_ID = new.user_ID;
END

Best Answer

I believe this is what you need:

CREATE Trigger upd_pt 
BEFORE UPDATE ON user
FOR EACH ROW 
SET NEW.point = NEW.votes + NEW.paymentAmt

This will set points before the user record is updated, so that total will be stored.

Based on code from the MySQL 5.7 Reference Manual.

NOTE: For best results, you probably also want to have this as an INSERT trigger, and may want to adjust if votes or paymentAmt can be NULL.