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

2021-12-24 13:58:25 浏览数 (1)

阅读量: 111

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

废话不说,直接上代码

table_name

表格名称

field

字段名称(删除删除的重复数据)

代码语言:javascript复制
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
)

0 人点赞