mysql怎么查询表内重复记录?mysql重复记录操作示例

发布时间:2020-07-23编辑:脚本学堂
本文介绍了mysql查询表内重复记录的方法,查找表中多余的重复记录,查找表中多个字段多余的重复记录等,需要的朋友参考下。

mysql查询表内重复记录方法示例。

1、查找表中多余的重复记录,重复记录是根据单个字段(myinfoId)来判断
 

复制代码 代码示例:
select * from myinfo
where myinfoId in (select myinfoId from myinfo group by myinfoId having count(myinfoId) > 1)

2、删除表中多余的重复记录,重复记录是根据单个字段(myinfoId)来判断,只留有一个记录
 

复制代码 代码示例:
delete from myinfo
where myinfoId in (select myinfoId from myinfo group by myinfoId having count(myinfoId) > 1)
and min(id) not in (select id from myinfo group by myinfoId having count(myinfoId )>1)

3、删除表中多余的重复记录(多个字段),只留有rowid最小的记录
 

复制代码 代码示例:
delete from vitae a
where (a.myinfoId,a.seq) in (select myinfoId,seq from vitae group by myinfoId,seq having count(*) > 1)
and rowid not in (select min(rowid) from vitae group by myinfoId,seq having count(*)>1)

4、查找表中多余的重复记录(多个字段)
 

复制代码 代码示例:
select * from vitae a
where (a.myinfoId,a.seq) in (select myinfoId,seq from vitae group by myinfoId,seq having count(*) > 1)

5、查找表中多余的重复记录(多个字段),不包含rowid最小的记录
 

复制代码 代码示例:
select * from vitae a
where (a.myinfoId,a.seq) in (select myinfoId,seq from vitae group by myinfoId,seq having count(*) > 1)
and rowid not in (select min(rowid) from vitae group by myinfoId,seq having count(*)>1)

例如,在A表中存在一个字段“name”, 而且不同记录之间的“name”值有可能会相同,需要查询出在该表中的各记录之间,“name”值存在重复的项;
 

复制代码 代码示例:
Select Name,Count(*) From A Group By Name Having Count(*) > 1

查性别,则sql语句为:
 

复制代码 代码示例:
Select Name,sex,Count(*) From A Group By Name,sex Having Count(*) > 1