仅将最新记录从一个 table 复制到另一个的存储过程

Stored procedure to copy only latest records from one table to another

我的来源 table TABLE_A 经常通过一些前端消费者应用每小时更新一次。 我有另一个报告 table TABLE_B 从 TABLE_A.

获取一些数据

最初通过插入语句手动复制记录。 现在这需要自动化,每 1 小时存储 procedure/script 运行s 并复制记录。所以当这个脚本执行第二个 运行 时,它不应该复制它已经在第一个 运行 中复制的记录。 请帮助我如何做到这一点。

TABLE_A:

id  date         product    type
11  2020-10-14    abc        T
12  2020-10-16    def        P
- - - - - - - - - -- - - - -- - - - -- - - -- 
13  2020-10-17    ghi        K
14  2020-10-18    klm        L
15  2020-10-19    abc        T

TABLE_B

id        date              product       type
hadgha     2020-10-14         abc          T
gsggss     2020-10-16         def          P 

假设脚本在其第一个 运行 中复制了前 2 条记录,然后将第 13 / 14 / 15 条记录添加到 TABLE_A。现在在第二个 运行 中,脚本应该将最后 3 条记录从 TABLE_A 复制到 TABLE_B..

假设id在两个table中都是主键,可以使用on duplicate key:

insert into table_b values(id, date, product, type)
select id, date, product, type
from table_a
on duplicate key update id = values(id)

基本上这会尝试 inserttable_atable_b 的所有内容;当满足 already-existing 记录时,查询转到 on duplicate key 子句,该子句执行“虚拟”更新(值已经相同,所以这是 no-op:MySQL 跳过更新)。

为了使其正常工作,我建议:

  • table_a 中的 id 设置为 int primary key auto_increment

  • table_b 中的 id 设置为 int primary key(不是自动递增,因为值总是从另一个 table 复制而来)

我建议使用子查询过滤掉它们,然后使用 on duplicate key:

insert into table_b values(id, date, product, type)
    select id, date, product, type
    from table_a a
    where a.date = (select max(a2.date) from table_a a2 where a2.type = a.type and a2.product = a.product)
    on duplicate key update id = values(id);

这假设您对 table_b(product, type).

有唯一约束

这些表没有任何重复键或任何共同约束。 正在创建的存储过程具有用于捕获最大计数 ID 的变量,然后在每个 运行.. 上更新此最大计数 ID。这样可以确保它复制了最后一个 运行.