IF语句触发语法错误

时间:2014-07-16 01:10:14

标签: mysql database database-design

我在PHPMyAdmin工作,我正在尝试添加触发器。由于语法错误(在触发器代码下面列出),我无法使触发器工作。

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行的“$$”附近使用正确的语法

1 个答案:

答案 0 :(得分: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