删除重复数据sql语句

比如一张简单的用户表

create table UserInfo
(
    Id int primary key identity(1,1),
    Name varchar(10) not null,
    CreateTime datetime
)

同一个用户名注册了多次,现在要删除这些重复的数据,保留最初注册的数据

delete from UserInfo 
where Name in(select Name from UserInfo group by Name having COUNT(*)>1) 
and id not in (select min(id) from UserInfo group by Name having COUNT(*)>1)

 

posted on 2013-06-08 17:28  DonnyPeng  阅读(164)  评论(0编辑  收藏  举报