MySQL 创建触发器时出现语法错误

MySQL syntax Error on Create Trigger

这是我的触发器,我收到 MySQL 语法错误。我想从 sms_index table sms_count column 值中减少余额。

 CREATE TRIGGER sms_log_update AFTER UPDATE ON sms_index
  FOR EACH ROW
  BEGIN
    IF NEW.approved_status = '1' THEN
      UPDATE  sms_package SET  balance =  (balance - OLD.sms_count) WHERE  group_id = OLD.ins_group_id;
    END IF;
  END;

错误信息:

 #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 5 

除了分隔符可能存在的问题外,您的代码看起来是正确的。请尝试以下操作:

DELIMITER //
CREATE TRIGGER sms_log_update AFTER UPDATE ON sms_index
FOR EACH ROW
BEGIN
    IF NEW.approved_status = '1' THEN
        UPDATE  sms_package SET  balance =  (balance - OLD.sms_count)
        WHERE  group_id = OLD.ins_group_id;
    END IF;
END;//

DELIMITER ;

来自documentation

However, just as for stored routines, if you use the mysql program to define a trigger that executes multiple statements, it is necessary to redefine the mysql statement delimiter so that you can use the ; statement delimiter within the trigger definition.