刪除mysql表部分關鍵字段重復的記錄

 

清理Statistic每天的重復數據【即Date Server Item SubItem 完全相同,Id肯定不同,Value可能相同】

先看一下Statistic表結構

 

 

 

 

 

處理樣本:

主要實現目的:

 

刪除Date Server Item SubItem 完全相同,Id肯定不同,Value可能相同的記錄

比如:

 2011-07-27 | mx1.dns.com.cn | SEND_MAIL | TOTAL                      | 14522 |          | 229         【刪除】

 2011-07-27 | mx1.dns.com.cn | SEND_MAIL | TOTAL                      | 14795 |          | 248         【保留】

實現過程:

 

第一步:創建與Statistic表結構完全相同的臨時表

use Statistic;

 

 

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

 

第二步:根據Id(自動增長)提取較新數據到臨時表

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

 

第三步:根據臨時表裡的數據的日期信息,將原表的對應日期的數據刪除

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

 

第四步:將臨時表裡的數據導入Statistic

insert into Statistic select * from  s_tmp;

 

第五步:最後清空臨時表

delete * from s_tmp;

 

 

 

 

實現結果:(去重後)

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *