今天遇到一个问题,数据表的数据有重复的,关键原因在于新增数据时,没有根据条件先判断数据是否存在,当数据存在时进行有关条件的更新,不存在时做新增数据。

对于表中已经存在的数据处理办法的方法:

1.先根据条件进行统计数据重复率。

2.查询那一些数据是重复的,将重复数据放在一张表中用于条件对比将源数据中的重复数据删除(mysql中使用一张表,sqlserver中可以使用临时表或者表变量存放数据)。

3.根据条件关联将重复数据删除。

可以先查询重复数据的总数:

select sum(count)
from(
select bidsectioncode,count(bidsectioncode) as count from businessdata.business_bidsection
group by bidsectioncode having count>1
order by create_time desc
) as d;

查出数据库中的重复记录的数据中的一条,这个不难,很简单的,sql语句如下:

select * from businessdata.business_bidsection
where bidsectioncode in (select bidsectioncode from businessdata.business_bidsection group by bidsectioncode having count(bidsectioncode) > 1)
and id in (select min(id) from businessdata.business_bidsection group by bidsectioncode having count(bidsectioncode)>1)

这些都是不重复的,换句话说都是要保留的,不被删掉的,而其余与这些结果中name相同的应该被删掉。

也就是说将上边那个sql语句id后边加一个not ,查出来的结果就是要删掉的。

select * from businessdata.business_bidsection
where bidsectioncode in (select bidsectioncode from businessdata.business_bidsection group by bidsectioncode having count(bidsectioncode) > 1)
and id not in (select min(id) from businessdata.business_bidsection group by bidsectioncode having count(bidsectioncode)>1)

我把这些需要删掉的存到另外一个表里,然后我新建一个business_bidsection_test表,结构复制business_bidsection的结构就好了。

复制一个创建语句到test表中。

然后将重复数据添加到test表中,插入语句是:

insert into business_bidsection_test
(select * from businessdata.business_bidsection
where bidsectioncode in (select bidsectioncode from businessdata.business_bidsection group by bidsectioncode having count(bidsectioncode) > 1)
and id not in (select min(id) from businessdata.business_bidsection group by bidsectioncode having count(bidsectioncode)>1));

那接下来做的就是删掉business_bidsection表里边与business_bidsection_test表的id相同的数据。

delete a.* from business_bidsection a, business_bidsection_test b where a.id = b.id ;

这样的结果就是完全不重复的,但是id是不连续的。

MySQL删除数据表中重复数据的更多相关文章

  1. 删除oracle 表中重复数据sql语句、保留rowid最小的一条记录

    delete from tablename a where rowid > ( select min(rowid) from table_name b where b.id = a.id and ...

  2. MySQL 数据库删除表中重复数据

    采集数据的时候,由于先期对页面结构的分析不完善,导致采漏了一部分数据.完善代码之后重新运行 Scrapy,又采集了一些重复的数据,搜了下删除重复数据的方法. N.B. 删除数据表的重复数据时,请先备份 ...

  3. Sql Server删除数据表中重复记录 三种方法

    本文介绍了Sql Server数据库中删除数据表中重复记录的方法. [项目]数据库中users表,包含u_name,u_pwd两个字段,其中u_name存在重复项,现在要实现把重复的项删除![分析]1 ...

  4. ROWID面试题-删除表中重复数据(重复数据保留一个)

    /* ROWID是行ID,通过它一定可以定位到r任意一行的数据记录 ROWID DNAME DEPTNO LOC ------------------ ------------------------ ...

  5. 向mysql数据表中插入数据失败的原因

    1.案例代码: $sql1="insert into content(category,subject,content,username,release_date) values('{$ca ...

  6. DataTable to Excel(使用NPOI、EPPlus将数据表中的数据读取到excel格式内存中)

    /// <summary> /// DataTable to Excel(将数据表中的数据读取到excel格式内存中) /// </summary> /// <param ...

  7. 将数据表中的数据添加到ComboBox控件中

    实现效果: 知识运用: ComboBox控件的DataSource 属性 //获取或设置ComboBox的数据源 public Object DataResouce{get;set;} //属性值:任 ...

  8. code first网站发布后数据表中没有数据问题

    code first网站发布后数据表中没有数据问题 (1).将internal sealed class Configuration类访问修饰符改为public  class Configuratio ...

  9. 查询和删除表中重复数据sql语句

      1.查询表中重复数据.select * from peoplewhere peopleId in (select   peopleId   from   people   group   by   ...

随机推荐

  1. Checked Exception & Unchecked Exception

    查Spring事务管理时看到一句话: Spring使用声明式事务处理,默认情况下,如果被注解的数据库操作方法中发生了unchecked异常,所有的数据库操作将rollback:如果发生的异常是chec ...

  2. [转载] PHP开发必看 编程十大好习惯

    适当抽象 但是在抽象的时候,要避免不合理的抽象,有时也可能造成过渡设计,现在只需要一种螺丝刀,但你却把更多类型的螺丝刀都做出来了(而且还是瑞士军刀的样子..): 一致性 团队开发中,可能每个人的编程风 ...

  3. e578. Setting the Clipping Area with a Shape

    This example demonstrates how to set a clipping area using a shape. The example sets an oval for the ...

  4. why pure virtual function has definition 为什么可以在基类中实现纯虚函数

    看了会音频,无意搜到一个frameworks/base/include/utils/Flattenable.h : virtual ~Flattenable() = 0; 所以查了下“纯虚函数定义实现 ...

  5. rsync文件同步、Inotify-tools参数详解

    inotifywait用于等待文件或文件集上的一个待定事件,可以监控任何文件和目录设置,并且可以递归地监控整个目录树: inotifywatch用于收集被监控的文件系统计数据,包括每个inotify事 ...

  6. 【转】Internet与Intranet区别

    提起Internet,大家都知道它是一个蓬勃发展的国际互联网. 而Intranet则是近两年才发展起来的新事物,通常被称作企业内部网. Internet是一组全球范围内信息资源的名字.这些资源非常巨大 ...

  7. VS 最近打开清理bat

    VS2008RecentCleaner.bat @echo off @REG Delete HKCU\Software\Microsoft\VisualStudio\9.0\FileMRUList / ...

  8. css两列等高布局

    布局方案 等高布局有几种不同的方法,但目前为止我认为浏览器兼容最好最简便的应该是padding补偿法.首先把列的padding-bottom设为一个足够大的值,再把列的margin-bottom设一个 ...

  9. 编写高性能的jQuery代码

    jQuery Optimization 现在jQuery已经出现在很多项目中,然而许多同学忽略了他的性能问题以及代码质量问题, 下面是我对jQuery的一些性能方面的学习. 选择器 选择器是jQuer ...

  10. CentOS7.1 Liberty云平台之Dashboard篇(7)

    控制节点: 一.安装及配置Dashboard 1.安装dashboard相关包 yum install openstack-dashboard 2.配置/etc/openstack-dashboard ...