How does MySQL delete records with duplicate key fields in a table?

Source: Internet
Author: User

MySQL databaseHow to delete some keyRepeated Fields? In this article, we use an example to introduce this deletion method. Let's talk about this example first.

First, let's take a look at the Statistic table structure:

Processing sample:

Main objectives:

Delete A Date Server Item SubItem with identical IDs and values.

For example:

| Mx1.dns.com.cn | SEND_MAIL | TOTAL | 14522 | 229 delete]

| Mx1.dns.com.cn | SEND_MAIL | TOTAL | 14795 | 248 reserved]

Implementation process:

Step 1: create a temporary table with the same structure as the Statistic table.

 
 
  1. use Statistic;  
  2.  
  3. create table s_tmp as select * from Statistic where 1=2; 

Step 2: extract new data to the temporary table based on the Id (auto-increment)

 
 
  1. insert into s_tmp select a.* from Statistic a,Statistic b where   
  2.  
  3. a.Date=b.Date and a.Server=b.Server and a.Key=b.Key and a.SubKey=b.SubKey and a.id > b.id; 

Step 3: delete the date data of the original table based on the date information of the data in the temporary table.

 
 
  1. delete  from Statistic where Date in (select distinct Date  from s_tmp ); 

Step 4: import the data in the temporary table to Statistic

 
 
  1. insert into Statistic select * from  s_tmp; 

Step 5: finally clear the temporary table

 
 
  1. delete * from s_tmp; 

Implementation Result: After deduplication)

This section describes how to delete some key fields in a MySQL table.

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.