dpd20130 2016-06-06 09:54
浏览 31

触发更新值mysql

I am quite new to SQL and am working on a small project, but I got stuck when wanting to implement triggers to my model.

I am designing a database for a sports league. I have a table "GamePlayed" with a column called "GoalsScored" and another one "Team" with a column called "GoalsFor". Those two tables are connected by the "TeamID" key.

I would like to have a trigger that updates the value of "GoalsFor" by adding to the previous value the value of "GoalsScored". In this way, with every game, the total quantity of goals scored will be constantly updated.

I tried the following:

CREATE TRIGGER "Goalsscored" AFTER UPDATE
ON `GamePlayed`
FOR EACH ROW
BEGIN
UPDATE `Team` SET `Team`.`GoalsFor`=`Team`.`GoalsFor`+`GamePlayed`.`GoalsScored` 
WHERE `GamePlayed`.`Team_TeamID`=`Team`.`TeamID`
END

Unfortunately, it says there is a problem in my SQL syntax... How can I do it?

  • 写回答

1条回答 默认 最新

  • douyinghuo8874 2016-06-06 10:20
    关注

    You need to change your delimiter. Something like this

    delimiter $$
    CREATE TRIGGER "Goalsscored" AFTER UPDATE
    ON `GamePlayed`
    FOR EACH ROW
    BEGIN
    UPDATE `Team` SET `Team`.`GoalsFor`=`Team`.`GoalsFor`+`GamePlayed`.`GoalsScored` 
    WHERE `GamePlayed`.`Team_TeamID`=`Team`.`TeamID`;
    END;
    $$
    

    See eg http://dev.mysql.com/doc/refman/5.7/en/trigger-syntax.html

    评论

报告相同问题?