本篇文章介紹mysql怎麼刪除重複資料給大家。有一定的參考價值,有需要的朋友可以參考一下,希望對大家有幫助。
sku對應的重量表,應該一個sku_id 對應唯一 一條重量數據,由於程式錯誤導致出現相同的冗餘資料。只保留一條,其他的刪除。
欄位 | 說明 |
---|---|
#id | 自增 |
sku_id | sku_id標識 |
假設表名: | weight
#查詢有重複資料的清單
SELECT sku_id,COUNT(id) FROM weight GROUP BY sku_id HAVING COUNT(sku_id) > 1
查詢重複資料裡面每個最小的id
SELECT min(id) FROM weight GROUP BY sku_id HAVING COUNT(sku_id) > 1
查詢去掉重複資料最小id的其他資料
SELECT id,sku_id FROM weight WHERE sku_id IN( SELECT sku_id FROM weight GROUP BY sku_id HAVING COUNT(sku_id) > 1)AND id NOT IN( SELECT MIN(id) FROM weight GROUP BY sku_id HAVING COUNT(sku_id) > 1)
刪除去掉重複資料最小id的其他資料資料
DELETE FROM weight WHERE sku_id IN( SELECT sku_id FROM weight GROUP BY sku_id HAVING COUNT(sku_id) > 1)AND id NOT IN( SELECT MIN(id) FROM weight GROUP BY sku_id HAVING COUNT(sku_id) > 1)
错误代码: 1093You can't specify target table 'weight' for update in FROM clause
DELETE FROM `weight` WHERE sku_id IN( SELECT sku_id FROM (SELECT sku_id FROM `weight` GROUP BY sku_id HAVING COUNT(sku_id) > 1) table1)AND id NOT IN ( SELECT id FROM (SELECT MIN(id) AS id FROM `weight` GROUP BY sku_id HAVING COUNT(sku_id) > 1) table2)
mysql影片教學
》以上是講解mysql如何刪除重複數據的詳細內容。更多資訊請關注PHP中文網其他相關文章!