1. 程式人生 > >SQL重復記錄查詢-count與group by having結合查詢重復記錄

SQL重復記錄查詢-count與group by having結合查詢重復記錄

nbsp 根據 iteye sele rul 判斷 select pro .net

查找表中多余的重復記錄,重復記錄是根據單個字段(peopleId)來判斷
技術分享圖片select * from people
技術分享圖片where peopleId in (select peopleId from people group by peopleId having count(peopleId) > 1)

例如: select * from testtable where numeber in (select number from people group by number having count(number) > 1 ) 可以查出testtable表中number相同的記錄

例如:

產品參數表rule_product_info 同一個申請單是否有多條記錄

用select app_no,count(1) from rule_product_info group by app_no having count(1)>1

2、刪除表中多余的重復記錄,重復記錄是根據單個字段(peopleId)來判斷,只留有rowid最小的記錄
技術分享圖片delete from people
技術分享圖片where peopleId in (select peopleId from people group by peopleId having count(peopleId) > 1)
技術分享圖片

and rowid not in (select min(rowid) from people group by peopleId having count(peopleId )>1)
技術分享圖片
技術分享圖片3、查找表中多余的重復記錄(多個字段)
技術分享圖片select * from vitae a
技術分享圖片where (a.peopleId,a.seq) in (select peopleId,seq from vitae group by peopleId,seq having count(*) > 1)
技術分享圖片
技術分享圖片4、刪除表中多余的重復記錄(多個字段),只留有rowid最小的記錄
技術分享圖片delete from vitae a
技術分享圖片where (a.peopleId,a.seq) in (select peopleId,seq from vitae group by peopleId,seq having count(*) > 1)
技術分享圖片
and rowid not in (select min(rowid) from vitae group by peopleId,seq having count(*)>1)
技術分享圖片
技術分享圖片
技術分享圖片5、查找表中多余的重復記錄(多個字段),不包含rowid最小的記錄
技術分享圖片select * from vitae a
技術分享圖片where (a.peopleId,a.seq) in (select peopleId,seq from vitae group by peopleId,seq having count(*) > 1)
技術分享圖片and rowid not in (select min(rowid) from vitae group by peopleId,seq having count(*)>1)

(二)
技術分享圖片比方說
技術分享圖片在A表中存在一個字段“name”,
技術分享圖片而且不同記錄之間的“name”值有可能會相同,
技術分享圖片現在就是需要查詢出在該表中的各記錄之間,“name”值存在重復的項;
技術分享圖片Select Name,Count(*) From A Group By Name Having Count(*) > 1
技術分享圖片
技術分享圖片如果還查性別也相同大則如下:
技術分享圖片Select Name,sex,Count(*) From A Group By Name,sex Having Count(*) > 1

SQL重復記錄查詢-count與group by having結合查詢重復記錄