MySQL 处理重复数据

删除重复记录

users: 表名
id:自增序号
uid:重复数据的判断字段
t1,t2: 为查询起的别名

DELETE
FROM
    users
WHERE
    uid IN (

        SELECT 
        *
    FROM (SELECT
            uid
        FROM
            users
        GROUP BY
            uid
        HAVING
            count(uid) > 1) t1
        
    )
AND id NOT IN (
    SELECT 
        *
    FROM (SELECT
        min(id)
    FROM
        users
    GROUP BY
        uid
    HAVING
        count(uid) > 1) t2
)

Add a Comment

邮箱地址不会被公开。 必填项已用*标注