我有一个从一个数据库读取并写入第二个数据库的迁移脚本。
我通常会更新现有记录。我如何记录更新,例如:
productID : 125
title : Product1 => test update
price : 125 => 140
这意味着 productID 125 的标题为“Products1”,更新后变为“test”,价格为“125”,变为“140”
一个想法是读取记录保留值然后更新,再次读取值并比较并记录必要的字段。
还有其他方法吗?
最佳答案
您可以使用触发器并将更改存储在另一个表中。
从我的头顶(以下假设 productId 永远不会更新);
create table main (
`id` int not null auto_increment,
`title` varchar(30) not null,
`price` float not null,
primary key(`id`)
);
create table logger (
`id` int not null auto_increment,
`productId` int not null,
`from_title` varchar(30) not null,
`to_title` varchar(30) not null,
`from_price` float not null,
`to_price` float not null,
primary key(`id`)
);
delimiter //
create trigger my_logger before update on main
begin
insert into
logger
set
`productId`=OLD.`id`,
`from_title`=OLD.`title`,
`to_title`=NEW.`title`,
`from_price`=OLD.`price`,
`to_price`=NEW.`title`;
end;//
delimiter ;
关于mysql - 记录 mysql 更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2666638/