2014-07-16 37 views
0

我在PHPMyAdmin中工作,我試圖添加一個觸發器。由於語法錯誤(在觸發器代碼下面列出),我無法使觸發器正常工作。使用IF語句觸發語法錯誤

delimiter $$ 
CREATE TRIGGER new_sub 
AFTER INSERT ON subscriptions 
FOR EACH ROW 
BEGIN 
IF NEW.cancelled = 0 THEN 
IF NEW.plan = 'pro' THEN 
    UPDATE statistics 
    SET statistics.premier_today = statistics.premier_today +1, 
     statistics.premier_this_week = statistics.premier_this_week+1, 
     statistics.premier_this_month = statistics.premier_this_month+1, 
     statistics.premier_all_time = statistics.premier_all_time+1, 
     statistics.revenue_today = statistics.revenue_today + 8, 
     statistics.revenue_this_week = statistics.revenue_this_week +8, 
     statistics.revenue_this_month = statistics.revenue_this_month +8; 
END IF; 
IF NEW.plan = 'single-event' THEN 
    UPDATE statistics 
    SET statistics.single_event_today = statistics.single_event_today +1, 
     statistics.single_even_this_week = statistics.single_event_this_week +1, 
     statistics.single_event_this_month = statistics.single_event_this_month +1, 
     statistics.single_event_all_time = statistics.single_event_all_time +1, 
     statistics.revenue_today = statistics.revenue_today + 25, 
     statistics.revenue_this_week = statistics.revenue_this_week +25, 
     statistics.revenue_this_month = statistics.revenue_this_month +25, 
     statistics.revenue_all_time = statistics.revenue_all_time +25; 
END IF; 
END IF; 
END;$$ 

1064 - 您的SQL語法錯誤;檢查對應於你的MySQL服務器版本正確的語法使用近「$$」第29行

+0

在存儲過程('delimiter $$')之前是否有'delimiter'語句? –

+0

我一開始沒有。我繼續進行修改,並且發現了上面發佈的另一個錯誤(UGH!)。越來越近,但仍然不夠。儘管到目前爲止,感謝您的所有幫助,但您一直在幫助很大。如果您有任何其他想法,請告訴我。 – mdobrenko

+0

最後兩行應該是'end;'和'delimiter $$'。 –

回答

0

case在選擇使用手冊。您將在觸發器中使用if。這是你想要的?

BEGIN 
    IF NEW.cancelled = 0 THEN 
    IF NEW.plan = 'pro' THEN 
     UPDATE statistics 
     SET statistics.premier_today = statistics.premier_today +1, 
      statistics.premier_this_week = statistics.premier_this_week+1, 
      statistics.premier_this_month = statistics.premier_this_month+1, 
      statistics.premier_all_time = statistics.premier_all_time+1, 
      statistics.revenue_today = statistics.revenue_today + 8, 
      statistics.revenue_this_week = statistics.revenue_this_week +8, 
      statistics.revenue_this_month = statistics.revenue_this_month +8; 
    END IF; 
    IF NEW.plan = 'single-event' THEN 
     UPDATE statistics 
     SET statistics.single_event_today = statistics.single_event_today +1, 
      statistics.single_even_this_week = statistics.single_event_this_week +1, 
      statistics.single_event_this_month = statistics.single_event_this_month +1, 
      statistics.single_event_all_time = statistics.single_event_all_time +1, 
      statistics.revenue_today = statistics.revenue_today + 25, 
      statistics.revenue_this_week = statistics.revenue_this_week +25, 
      statistics.revenue_this_month = statistics.revenue_this_month +25, 
      statistics.revenue_all_time = statistics.revenue_all_time +25; 
    END IF; 
    END IF; 
END 
+0

這就是我一直在尋找的東西。但是,我收到語法錯誤:您的SQL語法中有錯誤;檢查與您的MySQL服務器版本相對應的手冊,以便在'IF NEW.cancelled = 0 THEN IF NEW.plan ='pro'THEN UPDATE statistics 'at line 2 – mdobrenko

+0

附近使用正確的語法。觸發器定義的其餘部分。你應該編輯你的問題並插入整個定義。 –

+0

我繼續前進,並用我的整個定義替換它,併發布錯誤 – mdobrenko