SQL deletes duplicate records

Source: Internet
Author: User
This article provides a large number of SQL statement statements for deleting duplicate records. If you are worrying about how to delete duplicate data in the database, we provide you with a variety of SQL query statements for deleting duplicate records.

This article provides a large number of SQL statement statements for deleting duplicate records. If you are worrying about how to delete duplicate data in the database, we provide you with a variety of SQL query statements for deleting duplicate records.

SQL deletes duplicate records
This article provides a large number of SQL statements to delete duplicate records. If you are worrying about how to delete duplicate data, we provide you with a variety of SQL query statements to delete duplicate records.

Select .*
From temp a inner join (select name, sex from temp group by name, sex having count (*)> 1) B
On a. name = B. name and a. sex = B. sex

// View the instance for deleting duplicate records

// User table structure
Create table user (
Varchar (20) uname notnull;
Varchar (16) upass;
);


// Create a temporary table and copy non-duplicated data in the user table
Create table user_tmp as select distinct * from user;

Drop table user; // Delete the user table

Alert user_tmp rename user; // rename the user_tmp table to user

// ANOTHER METHOD

Delete ccs_users
Where userid in (
Select max (userid) from ccs_users group by userid having count (*)> 1)


It seems that one-person SQL deletes duplicate record instances.

For example:
Id? Name? Value
1? A? Pp
2? A? Pp
3? B? Iii
4? B? Pp
5? B? Pp
6? C? Pp
7? C? Pp
8? C? Iii
Id is the primary key.
Id? Name? Value
1? A? Pp
3? B? Iii
4? B? Pp
6? C? Pp
8? C? Iii
Method 1
Delete? Yourtable?
Where? [Id]? Not? In? (
Select? Max ([id])? From? Yourtable?
Group? By? (Name? +? Value ))

Method 2
Delete? A
From? Table? A? Left? Join (
Select? Id = min (id )? From? Table? Group? By? Name, value
) B? On? A. id = B. id
Where? B. id? Is? Null

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.