数据核对之找出增删数据

1、查询表结构,获取主键信息

SHOW CREATE TABLE 表名;

2、创建临时表并写入备份数据

create table 临时表 as
select *
from 主表
where expdate >=  '开始时间' and expdate < '结束时间';

3、重新从源系统采集数据至主表中

4、临时表中有的数据,但在主表中没有,即为源端删除的数据

select *
from 临时表 b
where b.expdate >=  '开始时间' and b.expdate < '结束时间'
and not exists (
    select 1
    from 主表 a
    where a.主键=b.主键
    and a.expdate >=  '开始时间' and a.expdate < '结束时间'
);

5、主表中有的数据,但在临时表中没有,即为源端新增的数据

select *
from 主表 b
where b.expdate >=  '开始时间' and b.expdate < '结束时间'
and not exists (
    select 1 from
    临时表 a
    where a.主键=b.主键
    and a.expdate >=  '开始时间' and a.expdate < '结束时间'
);

 

posted @ 2023-07-17 17:29  宜家数据小哥  阅读(17)  评论(0编辑  收藏  举报