2013-10-11 48 views
2

因此,我正在嘗試爲體育應用程序(特別是足球)構建一個Web應用程序。現在,我在記錄比賽得分後應該更新積分的觸發器遇到問題。在這個例子中,我有一個'遊戲'表和一個'積分榜'表,就像這樣。觸發器上的錯誤1054(42S22)

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,並蓋有外鍵組隊,現場,季節和排行榜,分別。

我想在遊戲更新後創建一個觸發器。我在這個應用程序中設計的目前設計是,當插入一個「遊戲」時,它還沒有被「播放」,當它被更新時,記錄得分,然後遊戲被認爲是「播放」。因此,這裏的觸發器的部分:

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的情況下執行兩個更新查詢。本質上,在觸發器的這個片段中,我正在顛倒之前的更新,然後實際上有更多的代碼使得更新的其餘部分發生。我對觸發器相當陌生,所以總是感激不盡。

回答

0

根據the documentation,MySQL中的IF塊終結符是兩個字:END IF

+0

奇怪的是,即使結束,如果,該行仍然吠叫我。 –

0

所以我在別人的幫助下得到了它。

我需要在觸發器周圍包裹一個分隔符,因爲第一個實例的;意味着整個觸發器「結束」,當然這不是我想要的。

如果你問我真的很愚蠢,但你能做什麼?

相關問題