資源描述:
《查詢和刪除同一表內(nèi)一個(gè)或多個(gè)字段重復(fù)記錄的SQL語句》由會(huì)員上傳分享,免費(fèi)在線閱讀,更多相關(guān)內(nèi)容在應(yīng)用文檔-天天文庫。
1、查詢和刪除同一表內(nèi)一個(gè)或多個(gè)字段重復(fù)記錄的SQL語句??比如現(xiàn)在有一人員表??(表名:peosons)若想將姓名、身份證號(hào)、住址這三個(gè)字段完全相同的記錄查詢出來select?p1.*?from?persons?p1,persons?p2?where?p1.id<>p2.id?and?p1.cardid?=?p2.cardid?and?p1.pname?=?p2.pname?and?p1.address?=?p2.address可以實(shí)現(xiàn)上述效果.幾個(gè)刪除重復(fù)記錄的SQL語句?1.用rowid方法2.用groupby方法3.用distinct方法
2、?1。用rowid方法據(jù)據(jù)oracle帶的rowid屬性,進(jìn)行判斷,是否存在重復(fù),語句如下:查數(shù)據(jù):????select*fromtable1awhererowid!=(select??max(rowid)??????fromtable1bwherea.name1=b.name1anda.name2=b.name2......)刪數(shù)據(jù):???delete??fromtable1awhererowid!=(select??max(rowid)??????fromtable1bwherea.name1=b.name1anda.name2=b.na
3、me2......)2.groupby方法查數(shù)據(jù): selectcount(num),max(name)fromstudent--列出重復(fù)的記錄數(shù),并列出他的name屬性 groupbynum havingcount(num)>1--按num分組后找出表中num列重復(fù),即出現(xiàn)次數(shù)大于一次刪數(shù)據(jù): deletefromstudent groupbynum havingcount(num)>1 這樣的話就把所有重復(fù)的都刪除了。3.用distinct方法-對于小的表比較有用createtabletable_newas??selectd
4、istinct*??fromtable1minuxtruncatetabletable1;insertintotable1select*fromtable_new;查詢及刪除重復(fù)記錄的方法大全1、查找表中多余的重復(fù)記錄,重復(fù)記錄是根據(jù)單個(gè)字段(peopleId)來判斷select*frompeoplewherepeopleIdin(select?peopleId?from?people?group?by?peopleId?having?count(peopleId)>1)2、刪除表中多余的重復(fù)記錄,重復(fù)記錄是根據(jù)單個(gè)字段(peopleId)來
5、判斷,只留有rowid最小的記錄deletefrompeoplewherepeopleId?in(select?peopleId?frompeople?group?by?peopleId??having?count(peopleId)>1)androwidnotin(selectmin(rowid)from?people?groupbypeopleId?havingcount(peopleId)>1)3、查找表中多余的重復(fù)記錄(多個(gè)字段)select*fromvitaeawhere(a.peopleId,a.seq)in?(selectpeo
6、pleId,seqfromvitaegroupbypeopleId,seq?havingcount(*)>1)4、刪除表中多余的重復(fù)記錄(多個(gè)字段),只留有rowid最小的記錄deletefromvitaeawhere(a.peopleId,a.seq)in?(selectpeopleId,seqfromvitaegroupbypeopleId,seqhavingcount(*)>1)androwidnotin(selectmin(rowid)fromvitaegroupbypeopleId,seqhavingcount(*)>1)5、查找表
7、中多余的重復(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)在就是需要查詢出在該表中的各記錄之
8、間,“name”值存在重復(fù)的項(xiàng);SelectName,Count(*)FromAGroupByNameHavingCount(*)>1如果還查性別也相同大則如下:S