How to remove duplicate data from a data table

Source: Internet
Author: User

We can use the keyword distinct to remove duplicate elements in the result set, but this does not delete repeated elements in the database.
If you want to delete data with repeated fields in a data table, it is a good way to use a temporary table as a transfer station.
Suppose we want to delete extra rows of data with duplicate values in blogurl fields in the data table tb_blogs (only one row of data with the same blogurl is retained), then we can do this:

Select * into # temp1 from tb_blogs where ID in (select max (ID) from tb_blogs group by blogurl)

This statement saves all the data that has not been duplicated to the temporary table # temp1 (note that my table has a primary key ID. If there is no primary key row, you should create a primary key in the temporary table, then, duplicate rows in the temporary table, and create another temporary table to save data without duplicate rows.) then

Drop table tb_blogs

Delete table tb_blogs

Select * into tb_blogs from # temp1

Reverse all data in the temporary table that does not have duplicate rows to tb_blogs.
Drop table # temp1
Delete temporary table

When I thought the above was a good method, the spider made me feel confused, and there was a better way:

Delete from tb_blogs where [ID] Not in (select max ([ID]) from tb_blogs group by [blogurl])

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.