oracle删除重复记录33738

上传人:大米 文档编号:554788587 上传时间:2022-09-19 格式:DOC 页数:3 大小:27.50KB
返回 下载 相关 举报
oracle删除重复记录33738_第1页
第1页 / 共3页
oracle删除重复记录33738_第2页
第2页 / 共3页
oracle删除重复记录33738_第3页
第3页 / 共3页
亲,该文档总共3页,全部预览完了,如果喜欢就下载吧!
资源描述

《oracle删除重复记录33738》由会员分享,可在线阅读,更多相关《oracle删除重复记录33738(3页珍藏版)》请在金锄头文库上搜索。

1、比如现在有一人员表(表名:peosons)若想将姓名、身份证号、住址这三个字段完全相同的记录查询出来selectp1.*frompersonsp1,personsp2wherep1.idp2.idandpl.cardid=p2.cardidandpl.pname=p2.pnameandpl.address=p2.address可以实现上述效果.几个删除重复记录的SQL语句用rowid方法1. 用groupby方法用distinct方法1。用rowid方法据据oracle带的rowid属性,进行判断,是否存在重复,语句如下:查数据:select*fromtablelawhererowid!=(s

2、electmax(rowid)fromtablelbwherea.name1=b.namelanda.name2=b.name2)删数据:deletefromtablelawhererowid!=(selectmax(rowid)fromtablelbwherea.name1=b.namelanda.name2=b.name2)groupby方法查数据:nameselectcount(num),max(name)fromstudent-歹U出重复的记录数,并歹U出他的属性groupbynumhavingcount(num)1-按num分组后找出表中num列重复,即出现次数大于一次删数据:del

3、etefromstudentgroupbynumhavingcount(num)1这样的话就把所有重复的都删除了。2. 用distinet方法-对于小的表比较有用createtabletable_newasselectdistinet*fromtablelminuxtruncatetabletablel;insertintotable1select*fromtable_new;查询及删除重复记录的方法大全1、查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断select*frompeoplewherepeopleIdin(selectpeopleIdfrompeopleg

4、roupbypeopleIdcount(peopleId)1)2、删除表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断,只留有最小的记录deletefrompeoplewherepeopleIdin(selectpeopleIdfrompeoplegroupbypeopleIdhavingcount(peopleId)1)androwidnotin(selectmin(rowid)frompeoplegroupbypeopleIdcount(peopleId)1)3、查找表中多余的重复记录(多个字段)select*fromvitaeawhere(a.peopleld,a.s

5、eq)in(selectpeopleld,seqfromvitaegroupbypeopleld,seqhavingcount(*)1)4、删除表中多余的重复记录(多个字段),只留有rowid最小的记录deletefromvitaeawhere(a.peopleId,a.seq)in(selectpeopleId,seqfromvitaegroupbypeopleId,seqhavingcount(*)1)androwidnotin(selectmin(rowid)fromvitaegroupbypeopleId,seqcount(*)1)5、查找表中多余的重复记录(多个字段),不包含rowi

6、d最小的记录select*fromvitaeawhere(a.peopleId,a.seq)in(selectpeopleId,seqfromvitaegroupbypeopleId,seqhavingcount(*)1)androwidnotin(selectmin(rowid)fromvitaegroupbypeopleId,seqhavingrowidhavinghavinghavingcount(*)1)(二)比方说在A表中存在一个字段namW,而且不同记录之间的namd值有可能会相同,现在就是需要查询出在该表中的各记录之间,“namd值存在重复的项;SelectName,Count(

7、*)FromAGroupByNameHavingCount(*)1如果还查性别也相同大则如下:SelectName,sex,Count(*)FromAGroupByName,sexHavingCount(*)1(三)方法一declaremaxinteger,idintegerdeclarecur_rowscursorlocalforselect主字段,count(*)from表名groupby主字段havingcount(*);1opencur_rowsfetchcur_rowsintoid,maxwhilefetch_status=0beginselectmax=max-1setrowcou

8、ntmaxdeletefrom表名where主字段=idfetchcur_rowsintoid,maxendclosecur_rowssetrowcount0方法二重复记录有两个意义上的重复记录,一是完全重复的记录,也即所有字段均重复的记录,二是部分关键字段重复的记录,比如Name字段重复,而其他字段不一定重复或都重复可以忽略。1、对于第一种重复,比较容易解决,使用selectdistinet*fromtableName就可以得到无重复记录的结果集。如果该表需要删除重复的记录(重复记录保留1条),可以按以下方法删除selectdistinet*into#TmpfromtableNamedrop

9、tabletableNameselect*intotableNamefrom#Tmpdroptable#Tmp发生这种重复的原因是表设计不周产生的,增加唯一索引列即可解决。2、这类重复问题通常要求保留重复记录中的第一条记录,操作方法如下假设有重复的字段为Name,Address,要求得到这两个字段唯一的结果集selectidentity(int,1,1)asautoID,*into#TmpfromtableNameselectmin(autoID)asautoIDinto#Tmp2from#TmpgroupbyName,autolDselect*from#TmpwhereautoIDin(se

10、lectautoIDfrom#tmp2)最后一个select即得到了NameAddress不重复的结果集(但多了一个autoID字段,实际写时可以写在select子句中省去此列)(四)查询重复select*fromtablenamewhereidin(selectidfromtablenamegroupbyidhavingcount(id)1)例子deletefromw_m_mjoutwhereksidin(selectksidfromtableNamegroupbyksidHAVINGCOUNT(ksid)1)androwidnotin(selectmin(ROWID)fromtableNamegroupbyksidHAVINGCOUNT(ksid)1)

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 办公文档 > 活动策划

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号