资源描述:
《几个删除重复记录的sql语句》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、几个删除重复记录的SQL语句几个删除重复记录的SQL语句1.用rowid方法2.用groupby方法3.用distinct方法1。用rowid方法据据oracle带的rowid属性,进行判断,是否存在重复,语句如下:查数据:select*fromtable1awhererowid!=(selectmax(rowid)fromtable1bwherea.name1=b.name1anda.name2=b.name2......)删数据:deletefromtable1awhererowid!=(selectmax(rowid)fromtable1bw
2、herea.name1=b.name1anda.name2=b.name2......)2.groupby方法查数据: selectcount(num),max(name)fromstudent--列出重复的记录数,并列出他的name属性 groupbynum havingcount(num)>1--按num分组后找出表中num列重复,即出现次数大于一次删数据: deletefromstudent groupbynum havingcount(num)>1 这样的话就把所有重复的都删除了。3.用distinct方法-对于小的表比较有用
3、createtabletable_newasselectdistinct*fromtable1minuxtruncatetabletable1;insertintotable1select*fromtable_new;查询及删除重复记录的方法大全1、查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断select*frompeoplewherepeopleIdin(selectpeopleIdfrompeoplegroupbypeopleIdhavingcount(peopleId)>1)2、删除表中多余的重复记录,重复记录是根
4、据单个字段(peopleId)来判断,只留有rowid最小的记录deletefrompeoplewherepeopleIdin(selectpeopleIdfrompeoplegroupbypeopleIdhavingcount(peopleId)>1)androwidnotin(selectmin(rowid)frompeoplegroupbypeopleIdhavingcount(peopleId)>1)3、查找表中多余的重复记录(多个字段)select*fromvitaeawhere(a.peopleId,a.seq)in(selectpeo
5、pleId,seqfromvitaegroupbypeopleId,seqhavingcount(*)>1)4、删除表中多余的重复记录(多个字段),只留有rowid最小的记录deletefromvitaeawhere(a.peopleId,a.seq)in(selectpeopleId,seqfromvitaegroupbypeopleId,seqhavingcount(*)>1)androwidnotin(selectmin(rowid)fromvitaegroupbypeopleId,seqhavingcount(*)>1)5、查找表中多余的重
6、复记录(多个字段),不包含rowid最小的记录select*fromvitaeawhere(a.peopleId,a.seq)in(selectpeopleId,seqfromvitaegroupbypeopleId,seqhavingcount(*)>1)androwidnotin(selectmin(rowid)fromvitaegroupbypeopleId,seqhavingcount(*)>1)(二)比方说在A表中存在一个字段“name”,而且不同记录之间的“name”值有可能会相同,现在就是需要查询出在该表中的各记录之间,“name”值
7、存在重复的项;SelectName,Count(*)FromAGroupByNameHavingCount(*)>1如果还查性别也相同大则如下:SelectName,sex,Count(*)FromAGroupByName,sexHavingCount(*)>1(三)方法一declare@maxinteger,@idintegerdeclarecur_rowscursorlocalforselect主字段,count(*)from表名groupby主字段havingcount(*)>;1opencur_rowsfetchcur_rowsinto@i
8、d,@maxwhile@@fetch_status=0beginselect@max=@max-1setrowcount@