经常要对数据库中的数据进行去重,有时还需要使用外部表填冲数据,本文档记录数据去重与外表填充数据。

date:2016/8/17

author:wangxl

1 需求

对user_info1表去重,并添加age项。

2 表数据

user_info1:
+----+----------+------+------+
| id | name | sex | age |
+----+----------+------+------+
| 1 | xiaolong | 1 | NULL |
| 2 | xiaoyun | 1 | NULL |
| 3 | xiaoqin | 2 | NULL |
| 4 | xiaolong | 1 | NULL |
| 5 | xiaodong | 1 | NULL |
| 6 | xiaokai | 1 | NULL |
| 7 | xiaohong | 2 | NULL |
| 8 | xiaolong | 1 | NULL |
| 9 | xiaohong | 2 | NULL |
| 10 | xiaofen | 2 | NULL |
+----+----------+------+------+ user_info2:
+----------+------+
| name | age |
+----------+------+
| xiaolong | 26 |
| xiaoyun | 28 |
| xiaoqin | 27 |
| xiaodong | 27 |
| xiaokai | 27 |
| xiaohong | 24 |
| xiaofen | 22 |
+----------+------+

3 实战

3.1 去重

(1) 找出有重复字段
select * from user_info1 where name in (select name from user_info1 group by name having count(name) > 1);
(2) 找出要删除的记录,重复记录是根据单个字段(name)来判断,只留有id最小的记录
select * from user_info1 where name in (select name from user_info1 group by name having count(name) > 1) and id not in (select min(id) from user_info1 group by name having count(name) > 1);
(3) 删除表中多余的重复记录
delete from user_info1 where name in (select name from user_info1 group by name having count(name) > 1) and id not in (select min(id) from user_info1 group by name having count(name) > 1); 报错:ERROR 1093 (HY000): You can't specify target table 'user_info1' for update in FROM clause

更换思路:找出每组中非最小id并删除,如下:

(4) 找出每组最小id
select min(id) from user_info1 group by name
(5) 找出每组非最小id
select * from user_info1 where id not in (select min(id) from user_info1 group by name);
(6) 删除每组中非最小id所在行
delete from user_info1 where id not in (select id from select min(id) from user_info1 group by name);
ERROR 1093 (HY000): You can't specify target table 'user_info1' for update in FROM clause
更正:
delete from user_info1 where id not in (select minid from (select min(id) as minid from user_info1 group by name) a);、 结果展示:
+----+----------+------+------+
| id | name | sex | age |
+----+----------+------+------+
| 1 | xiaolong | 1 | NULL |
| 2 | xiaoyun | 1 | NULL |
| 3 | xiaoqin | 2 | NULL |
| 5 | xiaodong | 1 | NULL |
| 6 | xiaokai | 1 | NULL |
| 7 | xiaohong | 2 | NULL |
| 10 | xiaofen | 2 | NULL |
+----+----------+------+------+

对于没有primary key的话,怎么去重呢?

(7) 创建表test
(8) insert into test select distinct(name),sex,age from user_info1 group by name; 暂时没想出一句话解决方案.

3.2 外表插入

update user_info1 t set age=(select age from user_info2 where name=t.name);
结果如下:
+----+----------+------+------+
| id | name | sex | age |
+----+----------+------+------+
| 1 | xiaolong | 1 | 26 |
| 2 | xiaoyun | 1 | 28 |
| 3 | xiaoqin | 2 | 27 |
| 5 | xiaodong | 1 | 27 |
| 6 | xiaokai | 1 | 27 |
| 7 | xiaohong | 2 | 24 |
| 10 | xiaofen | 2 | 22 |
+----+----------+------+------+

MYSQL数据去重与外表填充的更多相关文章

  1. mysql数据去重并排序使用distinct 和 order by 的问题

    比如直接使用: SELECT distinct mobileFROM table_aWHERE code = 123ORDER BY a_ime desc 在本地mysql数据库没有错,在线上的数据库 ...

  2. mysql 数据去重

    update ptop_investrecord set delflag = 1 where cid  = 250 and uid = 92569  and delflag = 0 and progr ...

  3. 设置MySQL数据表主键

    设置MySQL数据表主键: 使用“primary key”关键字创建主键数据列.被设置为主键列不允许出现重复的值,很多情况下与“auto_increment”递增数字相结合.如下SQL语句所示: My ...

  4. Pandas数据去重和对重复数据分类、求和,得到未重复和重复(求和后)的数据

    人的理想志向往往和他的能力成正比. —— 约翰逊 其实整个需求呢,就是题目.2018-08-16 需求的结构图: 涉及的包有:pandas.numpy 1.导入包: import pandas as ...

  5. PHPExcel使用-使用PHPExcel导出文件-导出MySQL数据

    现在数据库里面有一组数据,我们将它按照不同的难度进行分sheet. 首先我们需要写一个mysql的配置文件- db.config.php(utf-8编码) : <?php $dbconfig= ...

  6. MySQL数据库去重 SQL解决

    MySQL数据库去重的方法 ​ 数据库最近有很多重复的数据,数据量还有点大,本想着用代码解决,后来发现用SQL就能解决,这里记录一下 看这条SQL DELETE consum_record FROM ...

  7. MySQL的去重+列的表达式

    MySQL的去重+列的表达式 1. 去重 作用:去除SELECT查询出来的结果中重复的数据,重复的数据只显示一条. SELECT * FROM `repeat_num`                 ...

  8. Hadoop 中利用 mapreduce 读写 mysql 数据

    Hadoop 中利用 mapreduce 读写 mysql 数据   有时候我们在项目中会遇到输入结果集很大,但是输出结果很小,比如一些 pv.uv 数据,然后为了实时查询的需求,或者一些 OLAP ...

  9. Phantomjs+Nodejs+Mysql数据抓取(2.抓取图片)

    概要 这篇博客是在上一篇博客Phantomjs+Nodejs+Mysql数据抓取(1.抓取数据) http://blog.csdn.net/jokerkon/article/details/50868 ...

随机推荐

  1. Linux下使用多线程模拟异步网络通信

    服务器 /* socket server * 2014-12-15 CopyRight (c) arbboter */ #include <unistd.h> #include <s ...

  2. CloudStack cloud数据库op_host_capacity表type与控制板上的内容的对应关系

    listCapacity: type 名称 0 内存 1 CPU 3 主存储 4 公用IP地址 5 管理类IP地址 6 辅助存储 7 VLAN 9 本地存储 ViewResponseHelper.ja ...

  3. Effective Java实作toString() - 就是爱Java

    Object class中,也定义了toString()这个方法,因此所有的class也都继承这个方法.默认是传回这个对象完整类别名称,后面接一个"@",及一个不带正副号的十六进制 ...

  4. iPhone 和Android应用,特殊的链接:打电话,短信,email;

    http://ice-k.iteye.com/blog/1426526 下面的这篇文章主要是说,网页中的链接如何写,可以激活电话的功能. 例如,页面中展示的是一个电话号码,当用户在手机浏览器里面点击这 ...

  5. BZOJ 3550 Vacation

    http://www.lydsy.com/JudgeOnline/problem.php?id=3550 题意:有3N个数,你需要选出一些数,首先保证任意长度为N的区间中选出的数的个数<=K个, ...

  6. Android CursorAdapter

    CursorAdapter 继承于BaseAdapter是个虚类,它为cursor和ListView提供了连接的桥梁.            public abstract class     Cur ...

  7. libeXosip2(2-1) -- eXosip2 configuration API

    eXosip2 configuration API General purpose API. Data Structures struct   eXosip_dns_cache struct   eX ...

  8. unix c 02

    环境变量 - 存储在内存中的信息,格式是映射,作用就是 帮助系统 进行一些工作,一般是 查找某个东西. 预处理指令:#warning #error #pragma 使用程序直接调用库文件的函数(动态编 ...

  9. HOJ 1096 Divided Product (DFS)

    时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 Given two positive integers N and M, please divide N into sev ...

  10. 炫酷吊炸天的nodeppt

    由于要做一个关于node的分享,要准备写一个ppt方便就行交流.之前用的比较多的是slides(http://www.slides.com),最近知道了一个node写的工具,可以生成ppt,号称很强大 ...