MySQL 触发器:插入表 1,更新表 2
MySQL Trigger: INSERT table1, UPDATE table2
我想这样设置触发器:
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
中的一行。我应该使用两个单独的触发器来实现吗?
我当前的触发器不断抛出错误 "You have an error in your SQL syntax"。
您的 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;
我想这样设置触发器:
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
中的一行。我应该使用两个单独的触发器来实现吗?
我当前的触发器不断抛出错误 "You have an error in your SQL syntax"。
您的 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;