MySQL Delete duplicates only keep one bar

Source: Internet
Author: User
Tags mysql delete one table

The barcode value that would have been deleted in the Error_barcode table is repeated only one table structure as follows (Ps:id is self-growing, the figure is deleted successfully so ID is not contiguous)

The SQL statements are as follows:
DELETE fromError_barcodeWHEREBarCodeinch (        SELECTBarCode fromError_barcodeGROUP  byBarCode having            Count(BarCode)> 1    ) andId not inch (    SELECT        min(ID) fromError_barcodeGROUP  byBarCode having        Count(BarCode)> 1)

But it's a mistake.

Surfing the internet to find the next, this is a problem with MySQL, is no longer in the same statement to the same table first select some values in the update,

Then you need the handle. Queries are placed into a subquery

DELETE  fromError_barcodeWHEREBarCodeinch(    SelectA.barcode from(Select *  fromError_barcode) AGROUP  byBarCode having Count(BarCode)>1) andId not inch (    Select min(ID) from(Select *  fromError_barcode) bGROUP  byBarCode having Count(BarCode)>1)

Can see I put the following two sub-queries

SELECT             barCode        from            error_barcode        GROUP by             BarCode        have            count>1
SELECT        min (ID)      from         error_barcode    GROUPby        barcode    have        count>1

Change into

 select  a.barcode from  (select  *  from  Error _barcode) a group  by  barcode having  count  (barCode)   1  
Select min  from (Select* from Error_barcode) b  GROUPby havecount (BarCode)>1

It's all right.

MySQL Delete duplicates only keep one bar

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.