有人帮我写了以下内容:
CREATE TRIGGER update_stats AFTER UPDATE ON user_hours FOR EACH ROW
INSERT INTO hours_statistics (user_id, opportunity_id, completed_hours) VALUES
(OLD.user_id, OLD.opportunity_id, -OLD.completed_hours),
(NEW.user_id, NEW.opportunity_id, +NEW.completed_hours)
ON DUPLICATE KEY UPDATE
completed_hours = completed_hours + VALUES(completed_hours);
但是,触发器应仅针对以下情况运行:
UPDATE user_hours JOIN user_calendar USING (user_calendar_id, opportunity_id)
SET user_hours.completed_hours = agreed_hours,
user_hours.hours_committed = 'completed'
WHERE user_hours.hours_committed = 'accepted'
AND user_hours.completed_hours IS NULL
AND user_calendar.date_start = CURRENT_DATE();
就目前而言,如果手动调整管理员或用户的 completed_hours,更新触发器仍会运行。我想在代码中添加一些 IF 语句,但我的尝试都未能创建触发器。
我正在寻找类似的东西:
CREATE TRIGGER update_stats AFTER UPDATE ON user_hours FOR EACH ROW
IF hours_committed = 'completed' THEN
INSERT INTO hours_statistics (user_id, opportunity_id, completed_hours) VALUES
(OLD.user_id, OLD.opportunity_id, -OLD.completed_hours),
(NEW.user_id, NEW.opportunity_id, +NEW.completed_hours)
ON DUPLICATE KEY UPDATE
completed_hours = completed_hours + VALUES(completed_hours);
END IF
END
任何人都可以提供帮助,除非满足特定条件,否则不希望触发器运行,这让我们需要的代码工作很烦人,但这会在运行调整查询之外的几天内造成问题。
提前致谢。
标记