MySQL查询重复记录的方法
SQL #mysql2012-08-31 10:59
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)
相关文章
- in和exists的区别与SQL执行效率分析 2012/08/30
- Sql server常见性能问题的总结 2012/08/30
- Oracle查询表结构的一些相关sql语句 2012/07/12
- mysql的配置文件my.ini详解 2012/07/01
- mysql分页存储过程 2012/06/17
- Mysql导出表结构及表数据(mysqldump用法) 2012/06/17
- Mysql之Explain详解 2012/06/16
- mysql数据库表太大导致查询慢优化的几种方法 2012/06/16
- sql添加数据后返回受影响行数据 2012/05/21
- SQL批量插入数据的存储过程 2012/05/11