我有以下架构:SQL Fiddle。
触发器正在更新articles.votes 字段。我还需要使用与更新articles.votes 字段相同的公式来更新members.points 字段和memberslifetime_points 字段。我怎么做?
我有以下架构:SQL Fiddle。
触发器正在更新articles.votes 字段。我还需要使用与更新articles.votes 字段相同的公式来更新members.points 字段和memberslifetime_points 字段。我怎么做?
你在找这个吗?
DELIMITER $$
CREATE TRIGGER tg_ai_article_votes
AFTER INSERT ON article_votes
FOR EACH ROW
BEGIN
UPDATE articles
SET votes = COALESCE(votes, 0) + 1
WHERE id = NEW.article_id;
UPDATE members
SET points = COALESCE(points, 0) + 1,
lifetime_points = COALESCE(lifetime_points, 0) + 1
WHERE id = NEW.member_id;
END$$
CREATE TRIGGER tg_ad_article_votes
AFTER DELETE ON article_votes
FOR EACH ROW
BEGIN
UPDATE articles
SET votes = COALESCE(votes, 0) - 1
WHERE id = OLD.article_id;
UPDATE members
SET points = COALESCE(points, 0) - 1,
lifetime_points = COALESCE(lifetime_points, 0) - 1
WHERE id = OLD.member_id;
END$$
DELIMITER ;
这是SQLFiddle演示