Objective
This article mainly introduces to you about MySQL query, delete duplicate record of method, share out for everyone reference study, below to see detailed introduction:
Find records for all repeating headings:
?
1 |
select title, count (*) as count from user_table group by title having count >1; |
?
1 |
SELECT * FROM t_info a WHERE (( SELECT COUNT (*) FROM t_info WHERE Title = a.Title) > 1) ORDER BY Title DESC |
First, find duplicate records
1. Find all duplicate records
?
1 |
SELECT * FROM t_info a WHERE (( SELECT COUNT (*) FROM t_info WHERE Title = a.Title) > 1) ORDER BY Title DESC |
2. Filter duplicate records (show only one bar)
?
1 |
Select * From HZT Where ID In ( Select Max (ID) From HZT Group By Title) |
Note: The ID maximum one record is shown here
Second, delete duplicate records
1. Delete all duplicate records ( use caution )
?
1 |
Delete 表 Where 重复字段 In ( Select 重复字段 From 表 Group By 重复字段 Having Count (*)>1) |
2. Keep One (this should be what most people need ^_^)
?
1 |
Delete HZT Where ID Not In ( Select Max (ID) From HZT Group By Title) |
Note: the maximum one record for the ID is retained here
Iii. examples
1, look for redundant records in the table, duplicate records are based on a single field (Peopleid) to determine
?
1 |
select * from people where peopleId in ( select peopleId from people group by peopleId having count (peopleId) > 1) |
2, delete redundant records in the table, duplicate records are based on a single field (Peopleid) to judge, leaving only the smallest ROWID records
?
1 |
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. Find redundant duplicate records (multiple fields) in the table
?
1 |
select * from vitae a where (a.peopleId,a.seq) in ( select peopleId,seq from vitae group by peopleId,seq having count (*) > 1) |
4. Delete extra duplicate records (multiple fields) in the table, leaving only the record with ROWID minimum
?
1 |
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. Find redundant duplicate records (multiple fields) in the table, not including the smallest ROWID records
?
1 |
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)
|
Iv. Supplementary
There are more than two duplicate records, one is a completely duplicate record, that is, all the fields are duplicated records, and the second is some key field duplicate records, such as the Name field is repeated, and the other fields may not be repeated or repeated can be ignored.
1, for the first kind of repetition, easier to solve, using
?
1 |
select distinct * from tableName |
You can get a result set with no duplicate records.
If the table needs to delete duplicate records (duplicate records retain 1), you can delete them as follows
?
1234 |
select distinct * into #Tmp from tableName drop table tableName select * into tableName from #Tmp drop table #Tmp |
This duplication occurs because the table is poorly designed and the unique index columns are added to resolve.
2, this kind of repetition problem usually requires to keep the first record in the duplicate record, the operation method is as follows
Suppose there is a duplicate field name,address, which requires the result set to be unique for both fields
?
123 |
select identity(
int
,1,1)
as autoID, *
into #Tmp
from tableName
select min
(autoID)
as autoID
into #Tmp2
from #Tmp
group by Name
,autoID
select *
from #Tmp
where autoID
in
(
select autoID
from #tmp2)
|
Summarize
The above is the entire content of this article, I hope that the content of this article on everyone's study or work can bring some help
Original link: http://blog.phpsoho.com/2017/06/20/mysql-query duplicate record, delete duplicate record method Daquan/
How to query and delete duplicate records in MySQL