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

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

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

//删除用户根据用户名,并且不包括最小ID
delete from users where username in(
//根据用户名称排序,大于1
    select username from users 
    groupby username having count(username)>1
 
)and Id not in(
--根据用户名排序,获取最小ID
    select min(Id) from users 
    groupby username having count(username)>1
posted @ 2019-12-27 10:20  gaoyang666  阅读(703)  评论(0编辑  收藏  举报