Mysql 删除重复的数据,根据id索引(排除最小的数据)

注意:删除重复数据前,记得备份表!!!

废话不说,直接上代码

table_name 表格名称
field 字段名称(删除删除的重复数据)
DELETE
FROM table_name
WHERE id in (
#找出重复的数据,并且这个数据的id索引不是最小的
select b.number
from (SELECT bbb.id as number
FROM table_name as bbb
WHERE bbb.field IN (
SELECT field
FROM table_name
GROUP BY field
HAVING count(field) > 1
)
AND bbb.id not IN (
SELECT min(id)
FROM table_name
GROUP BY field
HAVING count(field) > 1
)) as b
)
暂无评论

发送评论 编辑评论


				
上一篇
下一篇