MySQL:根据 INSERT 中的 SET 列更改触发器定义

MySQL: Changing trigger definition based on the SET columns within an INSERT

我有两个插入语句:

INSERT INTO `users votes` SET `forumtopicid` = '$id'
INSERT INTO `users votes` SET `forumtopicid` = '$forumtopicid', `replyid` = '$id'

现在这是我当前的触发器定义:

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

我想更改它,如果 INSERT 包含 replyid,那么 UPDATE 将变为:

UPDATE `forum replies` fr
        SET votes = votes + 1
        WHERE NEW.replyid = fr.id

您将在触发器中使用 if 语句:

if new.replyid is null then
    UPDATE `forum topics` ft
            SET votes = votes + 1
            WHERE NEW.forumtopicid = ft.id;
else
    UPDATE `forum replies` fr
            SET votes = votes + 1
            WHERE NEW.replyid = fr.id;
end if;