mysql 删除重复数据

假设有 student 表一张, 有 id 、name 两个字段,name 一样即重复 。

一、查询是否有重复数据

SELECT name,count(*) from student GROUP BY `name` HAVING COUNT(*)>1;

二、删除重复数据

DELETE a
FROM
    student a
JOIN (
    SELECT
        MIN(id) id,   # 保留最小id的数据
        NAME,
        count(*)
    FROM
        student
    GROUP BY
        `name`
    HAVING
        COUNT(*) > 1
) b ON a. NAME = b. NAME
WHERE
    a.id > b.id;

你可能感兴趣的:(Mysql)