使用一个触发器更新两个表时出现语法错误

时间:2012-03-04 10:29:22

标签: mysql triggers

嗨我试图用一个触发器更新两个不同的表,但我不断收到语法错误,我不确定我做错了什么

  

1064 - 您的SQL语法出错;检查与MySQL服务器版本对应的手册,以获得正确的语法

     

靠近'UPDATE specialitys s

     

加入speciality_objects o         ON s.id = o.speciality'在第2行

如果我只是更新一个表,

触发器工作正常,但我不能将它们都放在一个触发器中

这是我的触发器

  DELIMITER $$
  CREATE TRIGGER after_user_type_or_status_change
  AFTER UPDATE
  ON users
  FOR EACH ROW

  BEGIN
  UPDATE stats s 

  SET

    ee_counter =
      CASE 
        WHEN  NEW.type= 1 THEN ee_counter + 1
        WHEN  NEW.type= 3  && OLD.type = 2 THEN ee_counter + 1
        WHEN  OLD.type= 1  && NEW.type != 3  THEN ee_counter - 1
        ELSE ee_counter 
      END ,

    er_counter =
      CASE 
        WHEN  NEW.type= 2  THEN er_counter + 1
        WHEN  NEW.type= 3  && OLD.type = 1 THEN er_counter + 1
        WHEN  OLD.type= 2  && NEW.type != 3  THEN er_counter - 1
        ELSE er_counter 
      END 

  WHERE
    s.id = 1;
END $$

BEGIN
UPDATE specialitys sp 

JOIN speciality_objects o
      ON sp.id = o.speciality_id

    JOIN users u
      ON o.user_id = u.id

 SET
    counter =
      CASE 
        WHEN  NEW.status = 1 THEN counter + 1
         ELSE counter 
      END 

  WHERE
    sp.id = o.speciality_id ;

END $$

DELIMITER ;

1 个答案:

答案 0 :(得分:0)

中间的END $$正在结束触发器定义。删除它和以下BEGIN