我正在 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 行“$$”附近使用的正确语法
最佳答案
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
关于mysql - IF 语句触发语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24770627/