2010-01-19 94 views
1

我正在嘗試設置一個MySQL觸發器,但我無法弄清楚如何得到我想要的結果。我想我需要設置一個MySQL @variable,但是我嘗試過的方法沒有奏效,我一直無法找到一個好的資源。我更改了phpMyAdmin中的分隔符,並且下面的註釋未在實際查詢中使用。MySQL @variable

CREATE TRIGGER coroner AFTER INSERT ON events 
FOR EACH ROW BEGIN 
UPDATE teams SET live = live-1 WHERE id = NEW.victim; 
UPDATE teams SET score = score+NEW.value WHERE id = NEW.shooter; 
UPDATE teams SET last_active = NEW.time WHERE id = NEW.shooter OR id = NEW.victim; 
// up to here all works fine 
IF (SELECT live FROM teams WHERE id = NEW.victim) = 0 THEN 
UPDATE teams SET slot = slot-1 WHERE slot > OLD.slot; 
ENDIF; 
END 

什麼我實際上在這裏做正在安排在一個圓圈一個團隊,而當時球隊的最後一名選手已經被淘汰了,我關閉了一圈沒有它。

下面是從phpMyAdmin的錯誤消息:

#1363 - There is no OLD row in on INSERT trigger

感謝您的幫助


新的嘗試:

CREATE TRIGGER coroner AFTER INSERT ON events 
FOR EACH ROW BEGIN 
UPDATE teams SET live = live-1 WHERE id = NEW.victim; 
UPDATE teams SET score = score+NEW.value WHERE id = NEW.shooter; 
UPDATE teams SET last_active = NEW.time WHERE id = NEW.shooter OR id = NEW.victim; 
SET @pivot := (SELECT slot FROM teams WHERE id = NEW.victim); 
IF (SELECT live FROM teams WHERE id = NEW.victim) = 0 THEN 
UPDATE teams SET slot = slot+1 WHERE slot < @pivot; 
ENDIF; 
END 

錯誤:

#1064 - 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 9

回答

1

觸發語言中沒有關鍵字ENDIF。改爲使用END IF(帶有空格)。

您可以查看IF Statement的語法文檔。

相關問題