Delete duplicate data in MySQL keep only one bar

Source: Internet
Author: User

With SQL statements, delete duplicates only keep one

In thousands of records, there are some same records, how can you use SQL statements, delete duplicates
1, look for redundant records in the table, duplicate records are based on a single field (Peopleid) to determine

Select    *from    peoplewhere    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

Deletefrom    peoplewhere    peoplename in (        SELECT            peoplename        from            people        GROUP            by Peoplename        have            count (peoplename) > 1    ) and Peopleid not in (    SELECT        min (peopleid)    From        people    GROUP by        peoplename    have        count (peoplename) > 1)

3. Find redundant duplicate records (multiple fields) in the table

Select    *from    vitae awhere    (A.peopleid, A.seq) in (        select            peopleid,            seq        from            Vitae        GROUP            by Peopleid,            seq        has            count (*) > 1    )

4. Delete extra duplicate records (multiple fields) in the table, leaving only the record with ROWID minimum

Deletefrom    vitae awhere    (A.peopleid, A.seq) in (        SELECT            peopleid,            seq        from            vitae        GROUP            by Peopleid,            seq        has            count (*) > 1    ) and rowID not in (    SELECT        min (rowid) C15/>from        vitae    GROUP        by Peopleid,        seq    has        count (*) > 1)

5. Find redundant duplicate records (multiple fields) in the table, not including the smallest ROWID records

Select    *from    vitae awhere    (A.peopleid, A.seq) in (        select            peopleid,            seq        from            Vitae        GROUP            by Peopleid,            seq        has            count (*) > 1    ) and rowID not in (    SELECT        min (rowid )    from        vitae    GROUP        by Peopleid,        seq    has        count (*) > 1)

6. Remove the first bit to the left of a field:

UPDATE Tablenameset [title]= Right ([title], (Len ([title])-1) "WHERE    title like ' Village% '

7. Remove the first bit to the right of a field:

UPDATE Tablenameset [title]= left ([title], (Len ([title])-1)) WHERE    Title like '% Village '

8. False Delete extra duplicate records (multiple fields) in the table, not including ROWID minimum records

UPDATE vitaeset ispass =-1WHERE    Peopleid in (        SELECT            peopleid        from            vitae        GROUP            by Peopleid

Delete duplicate data in MySQL keep only one bar

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.