具有IF和IF ELSE条件的Mysql触发器

时间:2017-11-15 11:12:36

标签: mysql if-statement triggers

我的表''评论'包含字段'type','receiver_id','confirm'和'id'。 有三种类型的对象,每种对象都有注释部分。 注释确认后,触发器会增加各自表中的对象数。

DB::unprepared('CREATE TRIGGER comment_confirm
        AFTER UPDATE ON comments FOR EACH ROW
        BEGIN
            IF OLD.confirm = 0 AND NEW.confirm = 1 THEN
                IF OLD.type = profile THEN
                    UPDATE profiles SET comments = comments + 1 WHERE user_id = OLD.reciever_id;
                ELSE IF OLD.type = blog THEN
                    UPDATE blogs SET comments = comments + 1 WHERE user_id = OLD.reciever_id;
                ELSE IF OLD.type = topic THEN
                    UPDATE topics SET comments = comments + 1 WHERE user_id = OLD.reciever_id;
                END IF;
            END IF;
        END
        ');

迁移错误是:

[PDOException]
  SQLSTATE[42000]: Syntax error or access violation: 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 '' at line 13

1 个答案:

答案 0 :(得分:1)

否则,如果应该是elseif(没有空格)如果OLD.type = profile看起来不正确应该用单引号进行字符串比较。