最近在公司遇到由于历史数据造成了有重复数据,导致有些操作无法进行,因此需要手动清理掉线上的重复数据,那么首先就是要查出来有哪些是重复的,下面这条sql就是查询重复数据的,而且根据sku把重复数据都显示在一起,这样一目了然。
select t.delivery_scope_id,t.goods_code,t.delivery_scope_group_id,t.priority_level,t.warehouse_name from delivery_scope t where (select count(*) from delivery_scope where is_delete=0 and
goods_code=t.goods_code and delivery_scope_group_id=t.delivery_scope_group_id and priority_level=t.priority_level)>1 and t.warehouse_id=40595316029788161 ORDER BY t.goods_code;
知道了重复数据,那么接下来就是调整数据,既然优先级有一样的,那么就把优先级一样的其中一条给加1
update delivery_scope set priority_level=priority_level+1 where delivery_scope_id in (
select * from (
select DISTINCT t.delivery_scope_id from delivery_scope t where
(select count(*) from delivery_scope where is_delete=0 and goods_code=t.goods_code and delivery_scope_group_id=t.delivery_scope_group_id and priority_level=t.priority_level)>1
and t.warehouse_id=40595316029788161
) b
);