1. 程式人生 > 其它 >sql查詢資料庫表中重複數值

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)