删除mysql表部分关键字段重复的记录
2011-07-27 14:22
411 查看
清理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;
实现结果:(去重后)
先看一下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;
实现结果:(去重后)
相关文章推荐
- 数据库删除完全重复和部分关键字段重复的记录
- Oracle删除部分字段重复和完全重复记录的方法
- 如何使用SQL删除某个字段重复的记录,保留其中一条
- 如何查出同一张表中字段值重复的记录,或者删除重复的记录
- 如何删除表记录中重复的字段
- Oracle 删除重复数据只留一条 查询及删除重复记录的SQL语句 1、查找表中多余的重复记录,重复记录是根据单个字段(Id)来判断 select * from 表 wher
- Sql Server表中删除重复记录最简单方法(多字段重复)
- Linux下删除大数据文件中部分字段重复行的方法
- mysql删除同一表中重复字段记录
- SQL重复记录查询 查询多个字段、多表查询、删除重复记录
- 查询|删除同一表内一个或多个字段重复记录的SQL语句
- MYSQL删除某个字段多余重复记录
- 如何快速删除Access指定字段的重复记录
- 删除某表中某字段重复的记录
- sql 删除表中多余的重复记录(多个字段),只保留一条记录
- 删除有两个字段相同的多条重复记录,只保存一条
- sql 删除部分字段重复数据
- sql 多个字段分组,删除重复记录,保留ID最小的一条
- 如何按字段删除重复记录
- mysql 查找重复的字段,并删除记录只保留一条