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) 原因是:删除这个表的同时又查询了这个表,查询这个表的同时又去删除了这个表,可以理解为死锁。mysql不支持这种删除查询同一张表的操作
错误代码: 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) 更新也和上面操作原理相同。