MySQL數(shù)據(jù)庫如何刪除表中部分關(guān)鍵字段重復(fù)的記錄
MySQL數(shù)據(jù)庫中如何刪除部分關(guān)鍵字段重復(fù)的記錄呢?本文我們通過一個(gè)例子來介紹這一刪除方法,接下來我們先說一說這個(gè)例子。
首先看一下Statistic表結(jié)構(gòu):
處理樣本:
主要實(shí)現(xiàn)目的:
刪除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 【保留】
實(shí)現(xiàn)過程:
***步:創(chuàng)建與Statistic表結(jié)構(gòu)完全相同的臨時(shí)表。
- use Statistic;
- create table s_tmp as select * from Statistic where 1=2;
第二步:根據(jù)Id(自動(dòng)增長)提取較新數(shù)據(jù)到臨時(shí)表
- 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;
第三步:根據(jù)臨時(shí)表里的數(shù)據(jù)的日期信息,將原表的對(duì)應(yīng)日期的數(shù)據(jù)刪除
- delete from Statistic where Date in (select distinct Date from s_tmp );
第四步:將臨時(shí)表里的數(shù)據(jù)導(dǎo)入Statistic
- insert into Statistic select * from s_tmp;
第五步:***清空臨時(shí)表
- delete * from s_tmp;
實(shí)現(xiàn)結(jié)果:(去重后)
關(guān)于刪除MySQL表部分關(guān)鍵字段重復(fù)的方法就介紹到這里了,如果您想了解更多關(guān)于MySQL數(shù)據(jù)庫的知識(shí),可以到這里看一下:http://database.51cto.com/mysql/,相信一定能夠帶給您收獲的。
【編輯推薦】


2010-09-26 17:24:24




