Question

So I am trying to create a trigger but I am getting a syntax error:

CREATE TRIGGER update_total_play_time AFTER INSERT ON play_log
FOR EACH ROW
    BEGIN
        UPDATE
            user u
        INNER JOIN
            play_log lp
        ON u.id = lp.user_id
        SET u.total_listened_seconds = u.total_listened_seconds + NEW.play_time
           WHERE u.id = NEW.user_id;
    END;

I cannot seem to understand why would there be an error? It says that the error is on the line where the WHERE u.id = NEW.user_id; is but if I remove it then it says that it is on the line before that.

Any help will be really appreciated.

Was it helpful?

Solution

Why do you need the join at all? Just use NEW:

CREATE TRIGGER update_total_play_time AFTER INSERT ON play_log
FOR EACH ROW
    BEGIN
        UPDATE user u
           SET u.total_listened_seconds = u.total_listened_seconds + NEW.play_time
           WHERE u.id = NEW.user_id;
    END;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top