SQL 触发器在 MySQL 中给出语法错误
SQL trigger giving syntax error in MySQL
DROP TRIGGER IF EXISTS demo_inc_when_viewstatus_one ;
DELIMITER $$
CREATE TRIGGER demo_inc_when_viewstatus_one AFTER UPDATE ON `tbl_ffa_demo`
FOR EACH ROW BEGIN
UPDATE `tbl_stats` SET open_demos=CASE WHEN NEW.demo_status=1 THEN
open_demos+1 ,
total_demos=total_demos+1 WHERE tbl_stats.area_id=NEW.territory AND NEW.view_status=1
END $$
DELIMITER;
我收到的错误消息是:
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 ' total_demos=total_demos+1 WHERE tbl_stats.area_id=NEW.territory AND NE' at line 4
您没有遵循 case 语句的语法:
case when <condition> then <true branch> else <false branch> end
所以,你应该有这样的东西:
... SET open_demos=CASE WHEN NEW.demo_status=1
THEN open_demos+1 ELSE open_demos=open_demos END, ...
DROP TRIGGER IF EXISTS demo_inc_when_viewstatus_one ;
DELIMITER $$
CREATE TRIGGER demo_inc_when_viewstatus_one AFTER UPDATE ON `tbl_ffa_demo`
FOR EACH ROW BEGIN
UPDATE `tbl_stats` SET open_demos=CASE WHEN NEW.demo_status=1 THEN
open_demos+1 ,
total_demos=total_demos+1 WHERE tbl_stats.area_id=NEW.territory AND NEW.view_status=1
END $$
DELIMITER;
我收到的错误消息是:
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 ' total_demos=total_demos+1 WHERE tbl_stats.area_id=NEW.territory AND NE' at line 4
您没有遵循 case 语句的语法:
case when <condition> then <true branch> else <false branch> end
所以,你应该有这样的东西:
... SET open_demos=CASE WHEN NEW.demo_status=1
THEN open_demos+1 ELSE open_demos=open_demos END, ...