0
我有以下架構:SQL Fiddle。如何更新觸發器中的多個表和字段?
觸發器正在更新articles.votes字段。我還需要它使用與更新articles.votes字段相同的公式更新members.points字段和成員lifetime_points字段。我怎麼做?
我有以下架構:SQL Fiddle。如何更新觸發器中的多個表和字段?
觸發器正在更新articles.votes字段。我還需要它使用與更新articles.votes字段相同的公式更新members.points字段和成員lifetime_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演示
是的,它沒有 - 我剛剛到現在 - 謝謝你對我的幫助。 – Padraig
@Padraig您絕對值得歡迎。祝你好運 :) – peterm