MySQL deletes repeated data in the database (whichever is the data)

Source: Internet
Author: User

Delete from ZQZRDP

where Tel in (select min (dpxx_id) from ZQZRDP GROUP BY Tel has count (tel) >1);

Run, error


The exception is: You cannot specify the update of the target table in the FROM clause. It's silly. MySQL writes like this, no, it makes people depressed.

It's just a step in the back of the egg.

The following is written by netizens. The same is the pit Daddy code, I can not execute on the machine.

1. Query the records that need to be deleted and keep a record.

  code such as the following copy code

select A.id,a.subject,a.receiver from Test1 a LEFT join (select C.subject,c.receiver, max (c.id) as  bid from Test1 C where s Tatus=0 GROUP by Receiver,subject have count (1) >1) b on a.id< b.bid where  A.subject=b.subject and A.receive R = B.receiver and a.ID < b.bid

2. Delete repeated records, leaving only one record. Attention. Subject,receiver to index. Otherwise it will be very slow.

  code such as the following copy code

delete A from Test1 A, (select C.subject,c.receiver, max (c.id) as  bid from test1 C where status=0 GROUP by Receiver,subject Having count (1) >1) b where a.subject=b.subject and a.receiver = B.receiver and a.ID < b.bid;


3. Find redundant and repeated records in the table. The repeated recording is based on a single field (Peopleid) to infer the

  code such as the following copy code

select * From people
where Peopleid in (select  peopleid  from  people  group  by  peopleId  ; having  count (Peopleid) > 1)


4. Delete Redundant records in the table, repeated records are inferred from a single field (Peopleid). Only the smallest record of rowid is left

  code such as the following copy code

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  g Roup by peopleid  have count (Peopleid) >1)

5. Delete Redundant records (multiple fields) in a table. Only the smallest record of rowid is left

Code such as the following Copy Code
Delete from Vitae a
where (A.PEOPLEID,A.SEQ) in (select Peopleid,seq from Vitae GROUP by PEOPLEID,SEQ have count (*) > 1)
and rowID not in (select min (rowid) from Vitae GROUP by PEOPLEID,SEQ have Count (*) >1)

MySQL deletes repeated data in the database (whichever is the data)

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.