1. 需求:后台给了一个EXCEL表,需要前端比对数据,筛选出差异数据。
2. 解决思路:
A 在数据库中建表tb_source,将excel 中的数据批量生成sql脚本 ,插入到数据库中。前端的数据表tb_barcode。利用tb_source、tb_barcode表left join 查询,筛选需要的数据。
B 利用tb_source、tb_barcode 表 set操作中的Minus–返回第一个查询有但第二个查询中不存在的所有唯一行记录。
- 示例演示
数据列A、B、C、列,在D1中输入,
=“INSERT INTO 表名 (字段1,字段2,字段3) VALUES(’”&A1&"’,’"&B1&"’,’"&C1&"’);"
选中D1,Ctrl+shift+end 选中要填充的区域,点 开始----填充—选择 -向下,向下填充完成。
将insert 语句脚本 在Oracle运行。将数据填充到数据表。
利用
INSERT into tb_barcode select tb_source.barcode from tb_source where rownum<1001;
运行语句
select * from tb_source left join tb_barcode on tb_source .barcode =tb_barcode.barcode;
查看运行结果。
上面查询结果中,最后一列null值,就是两个表差异的数据。
select count(*) from (select t1.tid, t2.barcode code2 from tb_source t1 left join tb_barcode t2 on t1.barcode =t2.barcode) res where res.code2 is not null;----将left join的结果作为表,统计不为null的数据量–这个就是两个表barcode 相同的数据量。
select count(*) from (select t1.tid, t2.barcode code2 from tb_source t1 left join tb_barcode t2 on t1.barcode =t2.barcode) res where res.code2 is null;
这个语句就是筛选出两张表差异的部分,然后可以利用此数据来同步数据表。
select * from (select t1.*,t2.barcode code2 from tb_source t1 left join tb_barcode t2 on t1.barcode =t2.barcode) res where res.code2 is null;
INTERSECT
select t1.barcode from tb_source t1 INTERSECT select t2.barcode from tb_barcode t2;----返回两个表都有的数据-
MINUS
select t1.barcode from tb_source t1 MINUS select t2.barcode from tb_barcode t2—返回两个表的差异数据集
select * from tb_source where barcode in (select t1.barcode from tb_source t1 MINUS select t2.barcode from tb_barcode t2) ;-----差异数据的全量数据
参考