SQL Delete duplicate records

Source: Internet
Author: User
The code is as follows Copy Code
Select A.*
From temp a INNER join (select Name,sex to Temp GROUP by Name,sex has count (*) >1) b
On a.name = b.name and a.sex = B.sex

To see a delete duplicate record instance

User table structure
CREATE TABLE User (
varchar (uname) notnull;
varchar (upass);
);

The code is as follows Copy Code


Create a temporary table and copy the data that is not duplicated in the user table
CREATE TABLE user_tmp as SELECT DISTINCT * from user;

drop table user;//Delete user table

Alert User_tmp Rename user;//renames the User_tmp table to user

Another way

The code is as follows Copy Code

Delete Ccs_users
where UserID in (
Select MAX (userid) from Ccs_users GROUP by UserID has count (*) >1)


Looks like a guy. SQL Delete duplicate record instance

  code is as follows copy code

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 primary key requirement You get the result
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.