如果線上資料庫有重複資料,怎麼處理?

2021-09-12 17:15:47 字數 1022 閱讀 6258

最近在公司遇到由於歷史資料造成了有重複資料,導致有些操作無法進行,因此需要手動清理掉線上的重複資料,那麼首先就是要查出來有哪些是重複的,下面這條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);

資料庫刪除重複資料

第一,資料庫中實體重複的解決方法。實體重複也就是完全重複 即表中兩行記錄完全一樣的情況。這類資料重複就需要刪除一條記錄,解決方法比較簡單,具體操作如下 使用select distinct from tablename就可以得到無重覆記錄的結果集。如果該錶需要刪除重複的記錄 重覆記錄保留1條 可以按以...

刪除資料庫重複資料

上圖是資料庫定義,資料中儲存了97萬條資料。我要刪除其中的的重複資料,並保留其中一條。其中,如果merchantid,commodityid,price,pricetime 只看天數 相同的話,那麼就進行刪除。delete from history where merchantid,commodit...

mysql資料庫去除重複資料

一 最原始的方法 delete from test where id not in select from select min id from test group by name as tmptable 刪除重複,留下id最小的資料 delete from test where id not i...