【问题标题】:MySQL - update rows if field records are not identicalMySQL - 如果字段记录不相同,则更新行
【发布时间】:2018-06-10 00:05:21
【问题描述】:

我的数据库中有两个表。 例如 t1 和 t2。 脚本每分钟更新一次 t1 记录。 如果 t1 记录(行)不存在或字段中的数据不相同,我需要将其复制到 t2。

以下是脚本更新 t1 表后我需要执行的步骤:

  1. 数据库将 t1 记录与 t2 进行比较
  2. 如果某些 t1 记录不相同/缺失,则在 t2 中插入/更新它。

这就像比分直播。我只需要更新/插入/删除受影响的行而不是整个表。

感谢您的帮助:)

【问题讨论】:

  • 第 1 步似乎是多余的

标签: mysql database


【解决方案1】:

为不同的动作创建两个触发器

用于更新操作

create trigger update_t1
after update  on t1 for each row
begin

select count(*) from t2 where id=New.id  into @num;

if @num=0 then
/*insert into t2; */

else
/*update t2 */
end if;
end;

用于插入操作

create trigger insert_t1
    after insert  on t1 for each row
    begin

    /*insert into t2; */

    end;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-20
    • 1970-01-01
    相关资源
    最近更新 更多