所以我想做的是为体育应用程序(特别是足球)构建一个 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 分别是球队、场、赛季和联赛表的外键。
我想在游戏更新后创建一个触发器。我在这个应用程序中的当前设计是,当一个“游戏”被插入时,它还没有被“玩”过,当它被更新时,然后记录分数,然后这个游戏被认为是“玩过”。所以这里是触发器的一部分:
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 一次完成两个更新查询。本质上,在触发器的这个片段中,我正在撤消先前的更新,然后实际上有更多的代码使其余的更新发生。我对触发器很陌生,所以总是很感激帮助。