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

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. 关于extern对变量的使用

    extern 是声明全局的变量的意思. 例如在一个工程中有两个cpp,一个是test.cpp一个是main.cpp . 我们在test.cpp中定义了一个int num;但是我们在main.cpp中想 ...

  2. CI框架uri去掉index.php

    CI框架的入口是index.php,所以url实际上要多出一个index.php,非常不美观.我使用的是apache服务器,要开启mod_rewrite服务才可以. sudo a2enmod rewr ...

  3. Python核心编程(第七章)--映像和集合类型

    字典:它是一个容器类型,能存储任意个数的Python对象,也包括其他容器类型,Python的字典是作为可变的哈希表实现的 映像类型中的数据是无序排列的   可以用工厂方法dict()来创建字典,也可以 ...

  4. 使用nRF51822/nRF51422创建一个简单的BLE应用 ---入门实例手册(中文)之五

    5应用测试 需要一个USB dongle与开发板evaluation kit,并配合Master Control Panel软件,以用于测试BLE应用.前期的准备工作在<nRF51822 Eva ...

  5. Nutshell.ThreadWorkerPool .Net线程池设计

    功能描述: 支持创建多个线程池,并统一管理 支持不同线程池的容量控制,以及最少活动线程的设置 支持不同线程池中活动线程的闲时设置,即线程空闲时间到期后即自动被回收 结构设计: ThreadWorker ...

  6. easyui 添加dialog

    javascript //查看角色所属用户 function roleuser(obj, id) { var C_ID = id; var Url = "/Sys/RoleUserName& ...

  7. 给div中动态添加节点并设置样式

    前端IOS今天需要动态的在图片前面添加一个按钮 主要是在使用 bt.setAttribute("class","aaa"); 可以对创建的节点使用setAttr ...

  8. php缓存技术常用函数

    OB缓存系列函数(输出缓存) ob_start()函数:打开输出缓冲区. 函数格式 ob_start(void) 说明:当缓冲区激活时,所有来自PHP程序的非文件头信息均不会发送,而是保存在ob缓冲区 ...

  9. cf C. Xenia and Weights

    http://codeforces.com/contest/339/problem/C #include <cstdio> #include <cstring> #includ ...

  10. Powershell 快捷键

    Powershell的快捷键和cmd,linux中的shell,都比较像. ALT+F7 清除命令的历史记录PgUp PgDn 显示当前会话的第一个命令和最后一个命令Enter 执行当前命令End 将 ...