MySQL Trigger:INSERT table1,UPDATE table2

时间:2015-01-02 12:57:42

标签: mysql sql triggers

我想设置一个触发器:

TRIGGER `increment_topic_replies_on_insert`
BEFORE INSERT ON  `forum replies` FOR EACH ROW

INSERT INTO `users votes`
SET userid = NEW.userid
    replyid = NEW.replyid,
    forumtopicid = NEW.forumtopicid;

UPDATE `forum topics` ft
SET replies = replies +1
WHERE NEW.forumtopicid = ft.id;

因此,在users votes进入forum topics之后,它会更新{{1}}中的一行。我应该使用两个单独的触发器来实现这一目标吗?

我当前的触发器不断抛出错误"您的SQL语法错误"。

1 个答案:

答案 0 :(得分:1)

你的INSERT& UPDATE语句语法错误,试试这个

CREATE TRIGGER `increment_topic_replies_on_insert` BEFORE INSERT ON `forum replies`
  FOR EACH ROW
  BEGIN
    INSERT INTO `users votes` (userid,replyid,forumtopicid)
    VALUES (NEW.userid, NEW.replyid, NEW.forumtopicid);

    UPDATE `forum topics` ft
      SET ft.replies = replies + 1
    WHERE ft.id = NEW.forumtopicid;
  END;