How to query and delete duplicate records in MySQL

Source: Internet
Author: User
Tags mysql query repetition

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 selecttitle,count(*) as count from user_table group by title havingcount>1;
?
1 SELECT* FROM t_info a WHERE ((SELECT COUNT(*) FROM t_info WHERE Title = a.Title) > 1) ORDER BYTitle 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 BYTitle DESC

2. Filter duplicate records (show only one bar)

?
1 Select* From HZT Where ID In (Select Max(ID) From HZT Group ByTitle)

Note: The ID maximum one record is shown here

Second, delete duplicate records

1. Delete all duplicate records ( use caution )

?
1 DeleteWhere 重复字段 In (Select 重复字段 From Group By 重复字段 HavingCount(*)>1)

2. Keep One (this should be what most people need ^_^)

?
1 DeleteHZT Where ID Not In (Select Max(ID) From HZT Group ByTitle)

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 havingcount(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 havingcount(*) > 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 selectdistinct * fromtableName

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 selectdistinct * 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.