MySQL query repeat deduplication and so on

Source: Internet
Author: User

Find records for all repeating headings:

[SQL]View PlainCopy
    1. SELECT * from t_info a where ((SELECT COUNT (*) from t_info where Title = A.title) > 1) Orde R by Title DESC

First, find duplicate records 1, find all duplicate records

[Plain]View PlainCopy
    1. SELECT * FROM table Where repeating field in (Select repeating field from table Group by repeating field having Count (*) >1)

2. Filter duplicate records (show only one bar)

[Plain]View PlainCopy
    1. SELECT * from Hzt Where ID in (the 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)

[Plain]View PlainCopy
    1. Delete table Where repeating field in (Select repeating field from table Group by repeating field having Count (*) >1)

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

[SQL]View PlainCopy
    1. Delete hzt Where ID not in (theSelect 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

[SQL]View PlainCopy
    1. SELECT * from people where Peopleid on (select Peopleid from people GROUP by Peopleid H Aving 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
[SQL]View PlainCopy
    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
[SQL]View PlainCopy
    1. SELECT * from vitae a where (A.PEOPLEID,A.SEQ) in (select Peopleid,seq from vitae Group by p Eopleid,seq Having count (*) > 1)

4. Delete extra duplicate records (multiple fields) in the table, leaving only the record with ROWID minimum [SQL]View PlainCopy
    1. delete from  vitae a where  (a.peopleid,a.seq)  in  ( span class= "keyword" >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

[Plain]View PlainCopy
    1. SELECT * from Vitae a WHERE (A.PEOPLEID,A.SEQ) in (select Peopleid,seq to Vitae GROUP by PEOPLEID,SEQ have Count (*) &G T 1) and rowID not in (select min (rowid) from Vitae GROUP by PEOPLEID,SEQ have 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

[SQL]View PlainCopy
    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
[SQL]View PlainCopy
    1. Select Distinct * to #Tmp from tableName
    2. Drop Table TableName
    3. SELECT * into tableName from #Tmp
    4. 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
[SQL]View PlainCopy
  1. Select Identity (int,1,1) as Autoid, * into #Tmp from tableName
  2. Select min (autoid) as autoid into #Tmp2 from #Tmp group by name,autoid
  3. SELECT * from #Tmp where autoid on (select autoid from #tmp2)

MySQL query repeat deduplication and so on

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.