如何创建获取值表格的触发器并更新其他表格

时间:2019-05-16 17:40:40

标签: mysql database-trigger

DELIMITER //
CREATE TRIGGER after_game_status AFTER UPDATE ON games
FOR EACH ROW 
BEGIN
    DECLARE player1,player2, score1, score2 INT DEFAULT NULL;
    IF ( NEW.status = 'finished') THEN
        SELECT account_id_player1, account_id_player2 INTO player1, player2 FROM games WHERE game_id = new.game_id;
        SELECT player1_score, player2_score INTO score1, score2 FROM scores WHERE game_id = new.game_id;
        IF( score1 > score2 ) THEN
            UPADTE games SET winner_account_id = player1 WHERE game_id = new.game_id;
        ELSE
            UPADTE games SET winner_account_id = player2 WHERE game_id = new.game_id;
        END IF;
    END IF;
END;
DELIMITER //
  

错误1064(42000):您的SQL语法有错误;检查与您的MariaDB服务器版本相对应的手册,以在游戏附近使用正确的语法SET winner_account_id = player1 WHERE game_id = new.game_id;第9行的ELSE UP'

1 个答案:

答案 0 :(得分:1)

有一个错字UPADTE,应该是UPDATE。在IFELSE的两个地方都有错字。

有效查询将是:

IF( score1 > score2 ) THEN
    UPDATE games SET winner_account_id = player1 WHERE game_id = new.game_id;
ELSE
    UPDATE games SET winner_account_id = player2 WHERE game_id = new.game_id;
END IF;