Sql 删除重复行记录

来源:互联网 发布:如何注销知乎 编辑:程序博客网 时间:2024/06/02 21:36

首先查询具有重复记录的所有记录

select iCityID,name,count(*) as count from testUpdate group by iCityID,name
having count(*)>1
order by count desc 

(1) 通过以下语句可以删除掉重复的记录

delete from testUpdate where exists(
select 1 from testUpdate t
where testUpdate.userid>t.userId and testUpdate.name=t.name and testUpdate.iCityID=t.iCityID)

(2)也可以通过这种方式删除重复记录
delete from TestUpdate where userID not in (select max(userid) from TestUpdate group by iCityID,name)

删除记录要小心操作,最好做个备份。

 

原创粉丝点击