資源描述:
《用sql刪除重復(fù)記錄的n種方法》由會(huì)員上傳分享,免費(fèi)在線閱讀,更多相關(guān)內(nèi)容在行業(yè)資料-天天文庫。
1、用SQL刪除重復(fù)記錄的N種方法[代碼]sql代碼deletetable1whererowidnotin(selectmax(rowid)fromtable1groupbycol1,col2)[代碼]某網(wǎng)友回復(fù)的。DELETEtablesWHERErowid<(SELECTMAX(rowid)FROMtableqWHEREq.name=s.name)用SQL刪除重復(fù)記錄的N種方法2010-02-1913:02例如:idnamevalue1app2app3biii4bpp5bpp6cpp7cpp8ciiiid是主鍵要求得到這樣的結(jié)果idnamevalue1app3biii4bpp6cpp8c
2、iii方法1deleteYourTablewhere[id]notin(selectmax([id])fromYourTablegroupby(name+value))方法2deleteafrom表aleftjoin(selectid=min(id)from表groupbyname,value)bona.id=b.idwhereb.idisnull查詢及刪除重復(fù)記錄的SQL語句查詢及刪除重復(fù)記錄的SQL語句1、查找表中多余的重復(fù)記錄,重復(fù)記錄是根據(jù)單個(gè)字段(peopleId)來判斷select*frompeoplewherepeopleIdin(selectpeopleIdfrompeo
3、plegroupbypeopleIdhavingcount(peopleId)>1)2、刪除表中多余的重復(fù)記錄,重復(fù)記錄是根據(jù)單個(gè)字段(peopleId)來判斷,只留有rowid最小的記錄deletefrompeoplewherepeopleIdin(selectpeopleIdfrompeoplegroupbypeopleIdhavingcount(peopleId)>1)androwidnotin(selectmin(rowid)frompeoplegroupbypeopleIdhavingcount(peopleId)>1)3、查找表中多余的重復(fù)記錄(多個(gè)字段)select*fro
4、mvitaeawhere(a.peopleId,a.seq)in(selectpeopleId,seqfromvitaegroupbypeopleId,seqhavingcount(*)>1)4、刪除表中多余的重復(fù)記錄(多個(gè)字段),只留有rowid最小的記錄deletefromvitaeawhere(a.peopleId,a.seq)in(selectpeopleId,seqfromvitaegroupbypeopleId,seqhavingcount(*)>1)androwidnotin(selectmin(rowid)fromvitaegroupbypeopleId,seqhavin
5、gcount(*)>1)5、查找表中多余的重復(fù)記錄(多個(gè)字段),不包含rowid最小的記錄select*fromvitaeawhere(a.peopleId,a.seq)in(selectpeopleId,seqfromvitaegroupbypeopleId,seqhavingcount(*)>1)androwidnotin(selectmin(rowid)fromvitaegroupbypeopleId,seqhavingcount(*)>1)(二)比方說在A表中存在一個(gè)字段“name”,而且不同記錄之間的“name”值有可能會(huì)相同,現(xiàn)在就是需要查詢出在該表中的各記錄之間,“name
6、”值存在重復(fù)的項(xiàng);SelectName,Count(*)FromAGroupByNameHavingCount(*)>1如果還查性別也相同大則如下:SelectName,sex,Count(*)FromAGroupByName,sexHavingCount(*)>1(三)方法一declare@maxinteger,@idintegerdeclarecur_rowscursorlocalforselect主字段,count(*)from表名groupby主字段havingcount(*)>;1opencur_rowsfetchcur_rowsinto@id,@maxwhile@@fetch
7、_status=0beginselect@max=@max-1setrowcount@maxdeletefrom表名where主字段=@idfetchcur_rowsinto@id,@maxendclosecur_rowssetrowcount0方法二”重復(fù)記錄”有兩個(gè)意義上的重復(fù)記錄,一是完全重復(fù)的記錄,也即所有字段均重復(fù)的記錄,二是部分關(guān)鍵字段重復(fù)的記錄,比如Name字段重復(fù),而其他字段不一定重復(fù)或都重復(fù)可以忽略?! ?、對(duì)于第