ERROR 1054(42S22)触发

时间:2013-10-11 05:15:01

标签: mysql triggers

所以我要做的是为体育应用程序(特别是足球)构建一个Web应用程序。现在,我遇到一个触发器,在记录比赛得分后应该更新积分榜。在这个例子中,我有一个'游戏'表和一个'standings'表,就像这样。

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 table的外键。

我想在游戏更新后创建一个触发器。我在这个应用程序中的目标设计是,当插入“游戏”时,它还没有被“播放”,当它被更新时,则记录得分,然后将游戏视为“播放”。所以这是触发器的一部分:

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来完成两个更新查询。本质上,在触发器的这个片段中,我正在反转先前的更新,然后有更多的代码实际上使更新的其余部分发生。我对触发器很新,所以总是很感激帮助。

2 个答案:

答案 0 :(得分:0)

根据the documentation,MySQL中的IF块终止符是两个单词:END IF

答案 1 :(得分:0)

所以我在其他人的帮助下得到了它。

我需要在触发器周围包装分隔符,因为第一个实例;意味着整个触发器“已经结束”,显然这不是我想要的。

如果你问我真的很蠢,但你能做什么?