sql查詢數據庫表中重復數值


sql查詢數據庫表中重復數值

-- 查詢表中id重復的值
select id from 表名 group by id having count(*) > 1

--查詢表中的重復記錄,重復記錄是根據id重復做判定
select * from 表名 where id in(select id from 表名 group by id having count(*) > 1)

-- 刪除表中多余的重復記錄,重復記錄根據id重復做判定,只留rowid最小的那條記錄
delete from 表名 where (id) in (select id from 表名 group by id having count(id) > 1) and rowid not in(select min(rowid) from group by id having count(*) > 1)

-- 查找表中多余的重復記錄(多個字段)
select * from 表名 a where (a.id,a.name) in (select id, name from 表名 group by id,name having count(*) > 1)

-- 查詢表中多余的重復記錄(多個字段),不包含rowid最小的記錄
select * from 表名 a where (a.id,a.name) in (select id,name from 表名 group by id,name having count(*) > 1) and rowid not in (select min(rowid) from 表名 group by id,name having count(*) > 1)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM