mysql - 触发错误 1054 (42S22)

标签 mysql triggers

所以我想做的是为体育应用程序(特别是足球)构建一个 Web 应用程序。现在,我在使用应该在记录比赛得分后更新排名的触发器时遇到了问题。在这个例子中,我有一个“游戏”表和一个“排名”表,就像这样。

mysql> describe game;
+-----------------+------------+------+-----+---------+-------+
| Field           | Type       | Null | Key | Default | Extra |
+-----------------+------------+------+-----+---------+-------+
| sid             | int(11)    | NO   | PRI | NULL    |       |
| fid             | int(11)    | NO   | PRI | NULL    |       |
| lid             | int(11)    | NO   | PRI | NULL    |       |
| htid            | int(11)    | NO   | PRI | NULL    |       |
| atid            | int(11)    | NO   | PRI | NULL    |       |
| date            | date       | NO   |     | NULL    |       |
| time            | time       | NO   |     | NULL    |       |
| h_g             | int(11)    | NO   |     | 0       |       |
| a_g             | int(11)    | NO   |     | 0       |       |
| has_been_played | tinyint(1) | NO   |     | 0       |       |
+-----------------+------------+------+-----+---------+-------+
10 rows in set (0.00 sec)

mysql> describe standings;
+-------+---------+------+-----+---------+-------+
| Field | Type    | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| tid   | int(11) | NO   | PRI | NULL    |       |
| sid   | int(11) | NO   | PRI | NULL    |       |
| lid   | int(11) | NO   | PRI | NULL    |       |
| pld   | int(11) | NO   |     | 0       |       |
| pts   | int(11) | NO   |     | 0       |       |
| h_w   | int(11) | NO   |     | 0       |       |
| h_t   | int(11) | NO   |     | 0       |       |
| h_l   | int(11) | NO   |     | 0       |       |
| h_gf  | int(11) | NO   |     | 0       |       |
| h_ga  | int(11) | NO   |     | 0       |       |
| a_w   | int(11) | NO   |     | 0       |       |
| a_t   | int(11) | NO   |     | 0       |       |
| a_l   | int(11) | NO   |     | 0       |       |
| a_gf  | int(11) | NO   |     | 0       |       |
| a_ga  | int(11) | NO   |     | 0       |       |
+-------+---------+------+-----+---------+-------+
15 rows in set (0.00 sec)

其中 h/atid(和 tid)、fid、sid 和 lid 分别是 team、field、season 和 league 表的外键。

我想在游戏更新后创建触发器。我在这个应用程序中的当前设计目标是,当插入“游戏”时,它还没有被“玩过”,当它被更新时,分数被记录下来,然后游戏被认为是“玩过”。所以这里是触发器的一部分:

CREATE TRIGGER `update_standing` AFTER UPDATE ON `game`
FOR EACH ROW
BEGIN
  # If a score has been recorded already, we'll reverse the old score
  # before updating the new score.
  IF OLD.has_been_played THEN
    # The Home Team previously recorded a win
    IF OLD.h_g > OLD.a_g THEN
      # Home win
      UPDATE standings
      SET pld = pld - 1,
          h_w = h_w - 1,
          pts = pts - 3,
          h_gf = h_gf - OLD.h_g,
          h_ga = h_ga - OLD.a_g
      WHERE tid = OLD.htid
        AND sid = OLD.sid
        AND lid = OLD.lid;

      # Away loss
      UPDATE standings
      SET pld = pld - 1,
          a_l = a_l - 1,
          a_gf = a_gf - OLD.a_g,
          a_ga = a_ga - OLD.h_g
      WHERE tid = OLD.atid
        AND sid = OLD.sid
        AND lid = OLD.lid;
    ENDIF;
  ENDIF;
END;

出于某种原因,我遇到了这些错误,但我不确定原因。

mysql> source new_trigger_myfam.sql
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 18
ERROR 1054 (42S22): Unknown column 'OLD.atid' in 'where clause'
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'ENDIF' at line 1
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'ENDIF' at line 1
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'END' at line 1

我的语法有明显错误吗?我知道我可以使用 case/when/then 在一个中执行两个更新查询。本质上,在触发器的这个片段中,我正在反转之前的更新,然后有更多的代码实际上使更新的其余部分发生。我对触发器还很陌生,因此我们非常感谢您的帮助。

最佳答案

根据 the documentation ,MySQL中的IF block 结束符是两个字:END IF

关于mysql - 触发错误 1054 (42S22),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19310703/

相关文章:

php - 如何从 INSERT 中的两个输入获取值?

silverlight - ControlTemplate.Triggers Silverlight 3 中的 WPF 等效项

sql-server - 触发和禁用 "row(s) affected"消息

sql-server - 使用 RAISERROR 和 ELSE 大小写问题触发

php - 使用ajax从数据库实时获取数据?

mysql - 用一个命令截断 MySQL 数据库中的所有表?

PHP MYSQL 分页列出所有字段

javascript - 当我尝试创建新表 php 时,ajax 调用失败

sql - Oracle变异触发器的解决方案

sql - 当所有其他列都相等时,对具有不同标签的值的约束