資源描述:
《sql刪除重復記錄的n種方法》由會員上傳分享,免費在線閱讀,更多相關內(nèi)容在行業(yè)資料-天天文庫。
1、SQL刪除重復記錄的N種方法--<一>:selectdistinct*into#tempfrom表truncatetable表insert表select*from#tempdroptable--<二>帶有標識列--備份數(shù)據(jù)select*into#tempfrom表altertable#tempdropcolumnid--刪除原表數(shù)據(jù)truncatetablemsgtable--恢復數(shù)據(jù)并去掉重復數(shù)據(jù)insertinto表selectdistinct*from#temp--<三>deleteFromaawhereain(se
2、lectaFromaagroupbyahavingcount(a)>1)--<四>altertable表addnewfieldintidentity(1,1)delete表wherenewfieldnotin(selectmin(newfield)from表groupby除newfield外的所有字段)altertable表dropcolumnnewfield--<五>--添加一個處理的標識字段altertable表addidintidentity(1,1)go???--刪除重復記錄deleteafrom表aleftjoi
3、n(selectid=min(id)from表groupbya,b)bona.id=b.idwhereb.idisnullgo--刪除處理用的標識字段altertable表dropcolumnid--參考:/*一張表里面以兩個字段為唯一字段,當幾條記錄的這兩個字段完全相同時,需要刪除重復項,如下表???a??b??c??d???1??2??3??4???1??5??3??5???1??2??7??9???以a、b為唯一字段,第一條和第三條的a、b完全相同,所以,需要刪除第一條記錄1??2??3??4??或者第三條記錄1??
4、2??7??9???即如下結(jié)果:???a??b??c??d???1??2??3??4???1??5??3??5???或???a??b??c??d???1??5??3??5???1??2??7??9??????請問各位大俠這種sql語句怎么寫*/????CREATETABLETb1(idint,[a]varchar(255),[b]varchar(255),[c]varchar(255),[d]varchar(255))INSERTTb1(id,[a],[b],[c],[d])SELECT1,'1','2','3','4'U
5、NIONALLSELECT2,'1','5','3','5'UNIONALLSELECT3,'1','2','7','9'UNIONALLSELECT4,'1','4','7','6'deleteTb1where[id]notin(selectmax([id])fromTb1groupbya,b)select*fromtb1droptabletb1如果要同時刪除第一和第三行即如下結(jié)果:abcd1535語句如下:deletemfromtbtinnerjoin(selecta,bfromtbgroupbya,bhavingco
6、unt(*)>1)nonm.a=n.aandm.b=n.b或delete*fromtbasm,(selecta,bfromtbgroupbya,bhavingcount(*)>1)nwherem.a=n.aandm.b=n.b--在幾千條記錄里,存在著些相同的記錄,如何能用SQL語句,刪除掉重復的呢?謝謝!1、查找表中多余的重復記錄,重復記錄是根據(jù)單個字段(peopleId)來判斷select*frompeoplewherepeopleIdin(selectpeopleIdfrompeoplegroupbypeopleId
7、havingcount(peopleId)>1)2、刪除表中多余的重復記錄,重復記錄是根據(jù)單個字段(peopleId)來判斷,只留有rowid最小的記錄deletefrompeoplewherepeopleIdin(selectpeopleIdfrompeoplegroupbypeopleIdhavingcount(peopleId)>1)androwidnotin(selectmin(rowid)frompeoplegroupbypeopleIdhavingcount(peopleId)>1)3、查找表中多余的重復記錄(
8、多個字段)select*fromvitaeawhere(a.peopleId,a.seq)in(selectpeopleId,seqfromvitaegroupbypeopleId,seqhavingcount(*)>1)4、刪除表中多余的重復記錄(多個字段),只留有rowid最小的記錄deletefr