mysql - IF 语句触发语法错误

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

最佳答案

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/

相关文章:

database - pg_hba.conf 中用于远程访问 postgresql 的 IP 范围

Android SQLite 显示错误

mysql - 最佳实践。如何存储用户搜索首选项

mysql - dbi 将 undef 视为空,但查询在 mysql 中产生结果

mysql - 由于搜索引擎大量抓取导致CPU高负载

php - 将 1 行,1 字段查询直接保存到变量中,而不是使用 while 循环

database-design - 什么时候应该使用复合主键?

php - zend 上的 mysql 数据库逻辑建议

mysql - SQL表逻辑,哪种方式是正确的

java - MyISAM 存储引擎在 Java Hibernate 中更可取吗?