日期:2014-05-16  浏览次数:20893 次

删除重复记录 但是要保留一条
现在发现混合数据很多的重复记录

id a
111 1
111 2

现在我需要保留任意一条 ,如111 1 这一个就可以
其他的要delete掉
如何写这个 delete语句?


------解决方案--------------------


SQL code
delete a from yourTable a,yourTable b where a.id=b.id and a.a>b.a;

------解决方案--------------------


这种情况,两条记录所有字段相同,没有办法进行删除!
只能

select distinct * from yourTable 
得到一个不重复的。然后清空原表。

可以

create table tttt as select distinct * from yourTable ;
delete from yourTable ;
insert into yourTable select * from ttt;


当您的问题得到解答后请及时结贴.
http://topic.csdn.net/u/20090501/15/7548d251-aec2-4975-a9bf-ca09a5551ba5.html