Delete duplicate data rows

Source: Internet
Author: User

Recently, there was a project that needed to capture data from the Internet. It was inevitable that duplicate data would be deleted and only one useful item would be left.

I have searched various forums, blogs, and well-known websites, but I have not found the method I want. (I am a lazy person and I like simple things, so I have been trying to find a method that can be done with an SQL statement .)

No way. You have to do it yourself. Then I went to w3cschool for further study. (W3cschool is a good place! Don't laugh at me. I never learn theoretical things or things that can be found in the dictionary .)

You can either say that the lazy person has a lazy way. It really made me come up. Let's see for yourself.

Delete from tablename where ID in (select max (ID) from tablename group by fieldname having count (0)> 1)

Let me explain:

First, the ID here is considered as the primary key.

Subquery divides data into groups of group by. If duplicate data exists, count (0)> 1.

The returned result is a set, which is the largest primary key max (ID) in each group ).

The main statement is a delete Statement, which is not special. The condition is in-type excessive consideration.

That's right. Experts have already seen it. Only one Data repeat can be deleted.

Previously said, I am lazy, diligent people can put him in the T-SQL loop, with subquery as a condition on the line. I don't have a large amount of data, which is enough.

Note that it is not original, but sharing. The use of temporary tables and tags on the internet is weak, but they do not despise, learn together, and learn together.

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.