A Simple Method for MYSQL to delete duplicate data

Source: Internet
Author: User
Tags mysql delete

Copy codeThe Code is as follows:
CREATETABLE 'users '(
'Id' int (10) NOTNULLAUTO_INCREMENT,
'Name' char (50) NOTNULL,
PRIMARYKEY ('id ')
)

Copy codeThe Code is as follows:
Deletefromuserswhereidin (selectmin (id) fromusersgroupbynamehavingcount (name)> 1 );

Error: 1093youcan tspecifytargettable ....

The reason is that the mysql Delete action cannot carry the query action of the current table. This means that you cannot delete the users table based on the users table information. Therefore, this statement reports an error and cannot be executed. You only need to create a temporary table as the query condition. As follows:

Copy codeThe Code is as follows:
Deletefromuserswhereidin (select * from (selectmin (id) fromusersgroupbynamehavingcount (name)> 1 ));

Also note that efromusers cannot use aliases here.

Other methods.
Copy codeThe Code is as follows:
Deleteusersasafromusersasa, (selectmin (id) id, namefromusersgroupbynamehavingcount (name)> 1
) Asbwherea. name = B. nameanda. id <> B. id;


Create a temporary table:
Copy codeThe Code is as follows:
Createtabletmp_usersselectmin ('id'), 'name' fromusersgroupbyname;

Truncatetableusers;
Insertintousersselect * fromtmp_users;
Droptabletmp_users;

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.