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.