MYSQL数据去重与外表填充
经常要对数据库中的数据进行去重,有时还需要使用外部表填冲数据,本文档记录数据去重与外表填充数据。
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数据去重与外表填充的更多相关文章
- mysql数据去重并排序使用distinct 和 order by 的问题
比如直接使用: SELECT distinct mobileFROM table_aWHERE code = 123ORDER BY a_ime desc 在本地mysql数据库没有错,在线上的数据库 ...
- mysql 数据去重
update ptop_investrecord set delflag = 1 where cid = 250 and uid = 92569 and delflag = 0 and progr ...
- 设置MySQL数据表主键
设置MySQL数据表主键: 使用“primary key”关键字创建主键数据列.被设置为主键列不允许出现重复的值,很多情况下与“auto_increment”递增数字相结合.如下SQL语句所示: My ...
- Pandas数据去重和对重复数据分类、求和,得到未重复和重复(求和后)的数据
人的理想志向往往和他的能力成正比. —— 约翰逊 其实整个需求呢,就是题目.2018-08-16 需求的结构图: 涉及的包有:pandas.numpy 1.导入包: import pandas as ...
- PHPExcel使用-使用PHPExcel导出文件-导出MySQL数据
现在数据库里面有一组数据,我们将它按照不同的难度进行分sheet. 首先我们需要写一个mysql的配置文件- db.config.php(utf-8编码) : <?php $dbconfig= ...
- MySQL数据库去重 SQL解决
MySQL数据库去重的方法 数据库最近有很多重复的数据,数据量还有点大,本想着用代码解决,后来发现用SQL就能解决,这里记录一下 看这条SQL DELETE consum_record FROM ...
- MySQL的去重+列的表达式
MySQL的去重+列的表达式 1. 去重 作用:去除SELECT查询出来的结果中重复的数据,重复的数据只显示一条. SELECT * FROM `repeat_num` ...
- Hadoop 中利用 mapreduce 读写 mysql 数据
Hadoop 中利用 mapreduce 读写 mysql 数据 有时候我们在项目中会遇到输入结果集很大,但是输出结果很小,比如一些 pv.uv 数据,然后为了实时查询的需求,或者一些 OLAP ...
- Phantomjs+Nodejs+Mysql数据抓取(2.抓取图片)
概要 这篇博客是在上一篇博客Phantomjs+Nodejs+Mysql数据抓取(1.抓取数据) http://blog.csdn.net/jokerkon/article/details/50868 ...
随机推荐
- C/C++中的函数传值
一.运行如下程序段 #include <iostream> #include <string> #include <cstring> //strcpy #inclu ...
- #能力开放平台系列-Fiddler访问Rest服务
问题 最近开发能力开放平台,需要将Dubbo服务转换成Rest服务,虽然转换很成功(后续文档会写出如何将Dubbo服务转换成Rest接口),但是调试起来特别的麻烦. 解决方案: Fiddler解决方案 ...
- mvc4 基于Area实现插件模块化开发
对于一个较大规模的Web应用,可以从功能上通过Area将其划分为为较小的单元.每个Area相当于一个独立的子系统,具有一套包含Model.Views和Controller在内 的目录结构和配置文件.一 ...
- Java笔记--File,FileInputStream,FileReader,InputStreamReader,BufferedReader 的使用和区别
转自:http://hi.baidu.com/danghj/item/0ef2e2c4ab95af7489ad9e39 参考资料: < core java > 12 章 使用 Java ...
- EF查询
public ActionResult AllSettings(DataSourceRequest command, Framework.Kendoui.Filter filter = null, S ...
- 【iOS开发】添加子控件方式(懒加载,GCC)
// // ViewController.m // GCC // // Created by admin on 15/10/7. // Copyright © 2015年 admin. All rig ...
- JAVA字符串转日期或日期转字符串
文章中,用的API是SimpleDateFormat,它是属于java.text.SimpleDateFormat,所以请记得import进 来! 用法: SimpleDateFormat sdf = ...
- 【LeetCode练习题】Recover Binary Search Tree
Recover Binary Search Tree Two elements of a binary search tree (BST) are swapped by mistake. Recove ...
- poj 1759 Garland (二分搜索之其他)
Description The New Year garland consists of N lamps attached to a common wire that hangs down on th ...
- PHP批量审核后台
/*批量审核方法*/ function setOn_all() { if($_POST) { $p=M('news'); $data=array(); $i=0; foreach ($_POST as ...