SQL 删除重复记录,只保留一条记录

2023-05-10 20:08:55 浏览数 (3)

删除表中多余的重复记录,重复记录是根据单个字段(id)来判断,只留有rowid最小的记录 

代码语言:javascript复制
//删除用户 根据用户名,并且不包括最小ID
delete from users where username in (
         --根据用户名称排序,大于1
    select username from users group by username having count(username) > 1

 ) and Id not in (
        --根据用户名排序,获取最小ID
    select min(Id) from users group by username having count(username) > 1
);

1 人点赞