linuxsir首页 LinuxSir.Org | Linux、BSD、Solaris、Unix | 开源传万世,因有我参与欢迎您!
网站首页 | 设为首页 | 加入收藏
您所在的位置:主页 > Linux基础建设 >

MySQL处理重复数据实例分析

时间:2019-05-22  来源:未知  作者:admin666

MySQL限制数据重复的方式:表上增加主键(Primary Key)或增加唯一性索引(Unique)

主键对重复资料进行限制,这样资料在导入时就无法重复插入

create table primary_t(
last_name varchar(20),
first_name varchar(20),
sex varchar(8),
primary key(last_name,first_name)
)engine=innodb default charset=utf8;



show table status like 'primary_t'\G

show index from primary_t\G


模拟插入两笔有主键约束的资料
insert into primary_t(last_name,first_name,sex) values('Frank','Li','Man'),('Frank','Li','Man'),('Coco','Li','Woman');

ERROR 1062 (23000): Duplicate entry 'Frank-Li' for key 'PRIMARY'


insert into primary_t(last_name,first_name,sex) values('Frank','Li','Man'),('Frank','Cai','Man'),('Coco','Li','Woman');

mysql> select * from primary_t;
+-----------+------------+-------+
| last_name | first_name | sex   |
+-----------+------------+-------+
| Coco      | Li         | Woman |
| Frank     | Cai        | Man   |
| Frank     | Li         | Man   |
+-----------+------------+-------+
rows in set (0.00 sec)

唯一性索引对复制资料进行限制

create table unique_t(
last_name varchar(20),
first_name varchar(20),
sex varchar(8),
unique index  unique_name(last_name,first_name)
)engine=innodb default charset=utf8;

show table status like 'unique_t'\G

show index from unique_t\G


模拟插入两笔有唯一性约束的资料
insert into unique_t(last_name,first_name,sex) values('Frank','Li','Man'),('Frank','Li','Man'),('Coco','Li','Woman');

ERROR 1062 (23000): Duplicate entry 'Frank-Li' for key 'unique_name'


insert into unique_t(last_name,first_name,sex) values('Frank','Li','Man'),('Frank','Cai','Man'),('Coco','Li','Woman');
mysql> select * from unique_t;
+-----------+------------+-------+
| last_name | first_name | sex   |
+-----------+------------+-------+
| Frank     | Li         | Man   |
| Frank     | Cai        | Man   |
| Coco      | Li         | Woman |
+-----------+------------+-------+
rows in set (0.00 sec)

对资料的统计

create table count_t(
last_name varchar(20),
first_name varchar(20),
sex varchar(8)
)engine=innodb default charset=utf8;

insert into count_t values('Frank','A','Man'),
('Frank','B','Man'),
('Frank','C','Woman'),
('Frank','D','Man'),
('Frank','E','Man'),
('Frank','F','Woman'),
('Frank','G','Man'),
('Frank','H','Man'),
('Frank','I','Woman'),
('Coco','A','Woman'),
('Coco','B','Man'),
('Coco','C','Man'),
('Coco','D','Man'),
('Coco','E','Man'),
('Coco','F','Man'),
('Coco','G','Woman'),
('Coco','H','Woman'),
('Alex','B','Man'),
('ALex','C','Man'),
('ALex','D','Man'),
('Alex','E','Man'),
('ALex','F','Man'),
('ALex','G','Woman'),
('ALex','H','Woman')
;

commit;

按各个列进行分组统计
select last_name,count(*) from count_t group by last_name;


select first_name,count(*) from count_t group by first_name;


select sex,count(*) from count_t group by sex;


mysql> select last_name,count(*) from count_t group by last_name;
+-----------+----------+
| last_name | count(*) |
+-----------+----------+
| Alex      |        7 |
| Coco      |        8 |
| Frank     |        9 |
+-----------+----------+
rows in set (0.00 sec)


mysql> select first_name,count(*) from count_t group by first_name;
+------------+----------+
| first_name | count(*) |
+------------+----------+
| A          |        2 |
| B          |        3 |
| C          |        3 |
| D          |        3 |
| E          |        3 |
| F          |        3 |
| G          |        3 |
| H          |        3 |
| I          |        1 |
+------------+----------+
rows in set (0.00 sec)


mysql> select sex,count(*) from count_t group by sex;
+-------+----------+
| sex   | count(*) |
+-------+----------+
| Man   |       16 |
| Woman |        8 |
+-------+----------+
rows in set (0.00 sec)


对分组的统计在进行限定,having 大于等于8次的记录
select last_name ,count(*) from count_t group by last_name having count(*) >=8;

mysql> select last_name ,count(*) from count_t group by last_name having count(*) >=8;
+-----------+----------+
| last_name | count(*) |
+-----------+----------+
| Coco      |        8 |
| Frank     |        9 |
+-----------+----------+
rows in set (0.00 sec)

对资料进行去重操作,主要使用distinct函数

select distinct last_name from count_t;
select distinct first_name from count_t;
select distinct sex from count_t;

对已有资料的表进行去重操作

insert into count_t values('Xi','Xi','Man'),('Xi','Xi','Man');


增加主键约束,加入ignore,忽悠重复的资料

alter ignore table count_t
add primary key(last_name,first_name);

select * from count_t where last_name='Xi';



mysql> alter ignore table count_t
    -> add primary key(last_name,first_name);
Query OK, 26 rows affected (0.01 sec)
Records: 26  Duplicates: 1  Warnings: 0

mysql> select * from count_t where last_name='Xi';
+-----------+------------+------+
| last_name | first_name | sex  |
+-----------+------------+------+
| Xi        | Xi         | Man  |
+-----------+------------+------+
row in set (0.00 sec)


使用group by方式也可去除重复资料

create table count_t0 as select last_name,first_name,sex from count_t group by last_name,first_name,sex;

Linux公社的RSS地址:https://www.linuxidc.com/rssFeed.aspx

上一篇:没有了
友情链接
  • Mozilla发布Firefox 67.0.4,修复沙箱逃逸漏洞
  • 蚂蚁金服正式成为CNCF云原生计算基金会黄金会员
  • Firefox 68将采用Microsoft BITS安装更新
  • OpenSSH增加对存储在RAM中的私钥的保护
  • 谷歌想实现自己的curl,为什么?
  • Raspberry Pi 4发布:更快的CPU、更大的内存
  • Firefox的UA将移除CPU架构信息
  • Ubuntu放弃支持32位应用程序实属乌龙,Steam会否重回Ubuntu怀抱
  • Qt 5.13稳定版发布:引入glTF 2.0、改进Wayland以及支持Lottie动
  • 红帽企业Linux 7现已内置Redis 5最新版
  • Slack进入微软内部禁用服务清单,GitHub也在其列?
  • 安全的全新编程语言V发布首个可用版本
  • Windows Terminal已上架,快尝鲜
  • 阿里巴巴微服务开源生态报告No.1
  • 面世两年,Google地球将支持所有基于Chromium的浏览器
  • 推进企业容器化持续创新,Rancher ECIC千人盛典完美收官
  • CentOS 8.0最新构建状态公布,或于数周后发布
  • Debian移植RISC
  • 微软拆分操作系统的计划初现雏形
  • Oracle发布基于VS Code的开发者工具,轻松使用Oracle数据库
  • Ubuntu 19.10停止支持32位的x86架构
  • 微软为Windows Terminal推出全新logo
  • 联想ThinkPad P系列笔记本预装Ubuntu系统
  • 微软发布适用于Win7/8的Microsoft Edge预览版
  • 启智平台发布联邦学习开源数据协作项目OpenI纵横
  • 经过六个多月的延迟,微软终于推出Hyper
  • ZFS On Linux 0.8.1 发布,Python可移植性工作
  • DragonFly BSD 5.6.0 发布,HAMMER2状态良好
  • Linux Kernel 5.2
  • CentOS 8.0 看起来还需要几周的时间
  • 百度网盘Linux版正式发布
  • PCIe 6.0宣布:带宽翻倍 狂飙至256GB/s
  • PHP 7.4 Alpha 发布,FFI扩展,预加载Opcache以获得更好的性能
  • Canonical将在未来的Ubuntu版本中放弃对32位架构的支持
  • Scala 2.13 发布,改进的编译器性能
  • 微软的GitHub收购了Pull Panda,并且使所有订阅完全免费
  • Windows Subsystem for Linux 2 (WSL 2)现在适用于Windows 10用
  • Debian 10 “Buster”的RISC
  • MariaDB宣布发布MariaDB Enterprise Server 10.4
  • DXVK 1.2.2 发布,带来微小的CPU开销优化
  • DragonFlyBSD 5.6 RC1 发布,VM优化,默认为HAMMER2
  • PrimeNG 8.0.0 发布,支持Angular 8,FocusTrap等
  • GIMP 2.10.12 发布,一些有用的改进
  • 清华大学Anaconda 镜像服务即将恢复
  • Debian GNU/Linux 10 “Buster” 操作系统将于2019年7月6日发布
  • 时时彩论坛
  • 五星体育斯诺克
  • 北单比分直播
  • 河北11选5走势图
  • 福建体彩36选7开奖结果
  • 九龙图库下载