1、查找表中多餘的重覆記錄,重覆記錄是根據單個欄位(Id)來判斷 select * from 表 where Id in (select Id from 表 group byId having count(Id) > 1) 2、刪除表中多餘的重覆記錄,重覆記錄是根據單個欄位(Id)來判斷,只留有ro ...
1、查找表中多餘的重覆記錄,重覆記錄是根據單個欄位(Id)來判斷
select * from 表 where Id in (select Id from 表 group byId having count(Id) > 1)
2、刪除表中多餘的重覆記錄,重覆記錄是根據單個欄位(Id)來判斷,只留有rowid最小的記錄
DELETE from 表 WHERE (判斷欄位) IN ( SELECT 判斷欄位 FROM 表 GROUP BY 判斷欄位 HAVING COUNT(判斷欄位) > 1) AND ROWID NOT IN (SELECT MIN(ROWID) FROM 表 GROUP BY id HAVING COUNT(*) > 1);
註意:AND後面的句子獲取到的是除去擁有rowid最小的所有的記錄(單個不重覆記錄也包含),Where 與 AND之間是獲取所有含有重覆記錄的欄位。
3、查找表中多餘的重覆記錄(多個欄位)
select * from 表 a where (a.Id,a.seq) in(select Id,seq from 表 group by Id,seq having count(*) > 1)
4、刪除表中多餘的重覆記錄(多個欄位),只留有rowid最小的記錄(原理同2)
delete from 表 a where (a.Id,a.seq) in (select Id,seq from 表 group by Id,seq having count(*) > 1) and rowid not in (select min(rowid) from 表 group by Id,seq having count(*)>1)
示例二:delete from student where s_id not in(select min(s_id) from student group by s_name,c_id);
5、查找表中多餘的重覆記錄(多個欄位),不包含rowid最小的記錄
select * from 表 a where (a.Id,a.seq) in (select Id,seq from 表 group by Id,seq having count(*) > 1) and rowid not in (select min(rowid) from 表 group by Id,seq having count(*)>1)