在面試的時候碰到一個問題,就是
寫一張表中有id和name 兩個字段,查詢出name重復的所有數據,現在列下:
select * from xi a where
(a.username) in (select username from xi group by username having count(*) > 1)
- 查詢出所有數據進行分組之后,和重復數據的重復次數的查詢數據,先列下
select count(username) as '重復次數',username from xi
group by username having count(*)>1 order by username desc
查詢及刪除重復記錄的方法大全
- 查找表中多余的重復記錄,重復記錄是根據單個字段(peopleId)來判斷
select * from people
where peopleId in
(select peopleId from people group by peopleId having count(peopleId) > 1)
- 刪除表中多余的重復記錄,重復記錄是根據單個字段(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)
- 查找表中多余的重復記錄(多個字段)
select * from vitae a
where (a.peopleId,a.seq) in (select peopleId,seq from vitae group by peopleId,seq having count(*) > 1)
- 刪除表中多余的重復記錄(多個字段),只留有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)
- 查找表中多余的重復記錄(多個字段),不包含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)
