资源描述:
《oracle删除重复记录.doc》由会员上传分享,免费在线阅读,更多相关内容在应用文档-天天文库。
1、比如现在有一人员表(表名:peosons)若想将姓名、身份证号、住址这三个字段完全相同的记录查询出来selectp1.*frompersonsp1,personsp2wherep1.id<>p2.idandp1.cardid=p2.cardidandp1.pname=p2.pnameandp1.address=p2.address可以实现上述效果.几个删除重复记录的SQL语句1.用rowid方法2.用groupby方法3.用distinct方法1。用rowid方法据据oracle带的rowid属性,进
2、行判断,是否存在重复,语句如下:查数据:select*fromtable1awhererowid!=(selectmax(rowid)fromtable1bwherea.name1=b.name1anda.name2=b.name2......)删数据:deletefromtable1awhererowid!=(selectmax(rowid)fromtable1bwherea.name1=b.name1anda.name2=b.name2......)2.groupby方法查数据: selectc
3、ount(num),max(name)fromstudent--列出重复的记录数,并列出他的name属性 groupbynum havingcount(num)>1--按num分组后找出表中num列重复,即出现次数大于一次删数据: deletefromstudent groupbynum havingcount(num)>1 这样的话就把所有重复的都删除了。3.用distinct方法-对于小的表比较有用createtabletable_newasselectdistinct*fromtab
4、le1minuxtruncatetabletable1;insertintotable1select*fromtable_new;查询及删除重复记录的方法大全1、查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断select*frompeoplewherepeopleIdin(selectpeopleIdfrompeoplegroupbypeopleIdhavingcount(peopleId)>1)2、删除表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断,只
5、留有rowid最小的记录deletefrompeoplewherepeopleIdin(selectpeopleIdfrompeoplegroupbypeopleIdhavingcount(peopleId)>1)androwidnotin(selectmin(rowid)frompeoplegroupbypeopleIdhavingcount(peopleId)>1)3、查找表中多余的重复记录(多个字段)select*fromvitaeawhere(a.peopleId,a.seq)in(selec
6、tpeopleId,seqfromvitaegroupbypeopleId,seqhavingcount(*)>1)4、删除表中多余的重复记录(多个字段),只留有rowid最小的记录deletefromvitaeawhere(a.peopleId,a.seq)in(selectpeopleId,seqfromvitaegroupbypeopleId,seqhavingcount(*)>1)androwidnotin(selectmin(rowid)fromvitaegroupbypeopleId,se
7、qhavingcount(*)>1)5、查找表中多余的重复记录(多个字段),不包含rowid最小的记录select*fromvitaeawhere(a.peopleId,a.seq)in(selectpeopleId,seqfromvitaegroupbypeopleId,seqhavingcount(*)>1)androwidnotin(selectmin(rowid)fromvitaegroupbypeopleId,seqhavingcount(*)>1)(二)比方说在A表中存在一个字段“name
8、”,而且不同记录之间的“name”值有可能会相同,现在就是需要查询出在该表中的各记录之间,“name”值存在重复的项;SelectName,Count(*)FromAGroupByNameHavingCount(*)>1如果还查性别也相同大则如下:SelectName,sex,Count(*)FromAGroupByName,sexHavingCount(*)>1(三)方法一declare@maxinteger,@idintegerdeclarecur_