How to delete multiple rows in a table with the same data but retain one

Source: Internet
Author: User

How does one Delete multiple rows of the same data in the same table but retain one? Delete a table with multiple rows of the same data, but keep one row of data, as shown in the following table:
ID name cdefine1 cdefine2 cdefine3

1 aa 101 09-08

2 AA 101 09-09

3 BB 101 09-09

4 cc 204 09-12

5 cc 204 09-13

6 dd 255 09-05

7 EE 255 09-06

8 EE 211
Now I want to delete the data with the same name and cdefine1 fields in the table. That is to say, if the name and cdefine1 fields are the same, delete them, but keep one. The final result is as follows:
ID name cdefine1 cdefine2 cdefine3

1 aa 101 09-08

2 BB 101 09-09

3 cc 204 09-12

4 dd 255 09-05

5 ee 255 09-06

8 EE 211
 
------ Solution --------------------------------------------------------

SQL code
Delete tfrom TB twhere exists (select 1 from TB where name = T. Name and cdefine1 = T. cdefine1 and ID> T. ID) ------ solution --------------------------------------------------------
SQL code
delete tfrom  tb twhere  exists (select 1 from tb where name=t.name and cdefine1=t.cdefine1 and id>t.id)

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.