天天看點

用SQL語句,删除掉重複的記錄

1、查找表中多餘的重複記錄,重複記錄是根據單個字段(peopleId)來判斷

select * from people 
where peopleId in (select peopleId from people group by peopleId having count(peopleId) > 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”值存在重複的項;

如果還查性别也相同大則如下:

ps:篩選出指定月,指定日,指定年的方法:

SELECT * FROM dbo.v_CashmereInBodyDetails 
WHERE YEAR(c_time)='2010' AND MONTH(c_time)=6 AND DAY(c_time)=8
           
篩選出2010年6月8日的資料