首页 > 解决方案 > 如何仅在包含更改时才使用触发器在历史表中添加数据

问题描述

有一个 python 脚本可以更新 MySQL 表中的数据。问题是当没有对数据进行更改时,触发器会复制 history_table 中的每条记录。

create trigger organizations_history_update after update on organizations for each row 
begin

declare N DATE;
set N = now();

UPDATE organizations_history
SET EndDate = N
WHERE id = OLD.id
AND EndDate IS NULL;

insert into organizations_history (id, name,current_version, db_maintenance_plan, custom_localization, custom_skin_for_ipad, custom_server_version, zendesk_id, StartDate, EndDate)
values (new.id, new.name, new.current_version, new.db_maintenance_plan, new.custom_localization, new.custom_skin_for_ipad, new.custom_server_version, new.zendesk_id, N, NULL);
end;

标签: mysqltriggers

解决方案


推荐阅读