代码之家  ›  专栏  ›  技术社区  ›  ntan

记录mysql更新

  •  1
  • ntan  · 技术社区  · 15 年前

    我有一个迁移脚本,从一个数据库读取数据,然后写入第二个数据库。

    productID : 125
    title : Product1 => test update
    price : 125 => 140
    

    这意味着productID 125的标题是“Products1”,更新后变成了“test”,价格是“125”,变成了“140”

    还有其他方法吗?

    1 回复  |  直到 9 年前
        1
  •  2
  •   Björn    15 年前

    从我的头顶(下面假设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 ;