博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
MySQL查询、删除重复记录
阅读量:6620 次
发布时间:2019-06-25

本文共 3953 字,大约阅读时间需要 13 分钟。

hot3.png

表结构,数据如下:

CREATE TABLE `redis_num_mem` (  `id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT '自增id',  `cluster_id` int(10) unsigned NOT NULL DEFAULT 0 COMMENT 'Redis集群id',  `type` varchar(10) NOT NULL DEFAULT '' COMMENT 'key类型',  `number` int(10) unsigned NOT NULL DEFAULT 0 COMMENT 'key的个数',  `memory` bigint(20) unsigned NOT NULL DEFAULT 0 COMMENT 'key的存内,单位bytes',  `create_time` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp() COMMENT '记录生成时间',  PRIMARY KEY (`id`)) ENGINE=InnoDB AUTO_INCREMENT=103 DEFAULT CHARSET=utf8 COMMENT='redis类型表'MariaDB [test]> select * from redis_num_mem;+-----+------------+--------+--------+--------+---------------------+| id  | cluster_id | type   | number | memory | create_time         |+-----+------------+--------+--------+--------+---------------------+|   1 |          1 | hash   |  47079 |    100 | 2018-10-22 23:48:12 ||   2 |          1 | set    |  20608 |    100 | 2018-10-22 23:48:12 ||   3 |          1 | list   |   3938 |    100 | 2018-10-22 23:48:12 ||   4 |          1 | string |  17662 |    100 | 2018-10-22 23:48:12 ||   5 |          1 | hash   |  46808 |    100 | 2018-10-22 23:48:12 ||   6 |          1 | set    |  20854 |    100 | 2018-10-22 23:48:12 ||   7 |          1 | string |  17710 |    100 | 2018-10-22 23:48:12 ||   8 |          1 | list   |   3917 |    100 | 2018-10-22 23:48:12 ||   9 |          1 | list   |   3854 |    100 | 2018-10-22 23:48:12 ||  10 |          1 | string |  17651 |    100 | 2018-10-22 23:48:12 ||  11 |          1 | hash   |  46960 |    100 | 2018-10-22 23:48:12 ||  12 |          1 | set    |  20654 |    100 | 2018-10-22 23:48:12 ||  14 |          2 |        |      0 |      0 | 2018-10-23 10:34:54 ||  15 |          2 |        |      0 |      0 | 2018-10-23 10:35:42 ||  16 |          0 |        |      0 |      0 | 2018-10-23 10:50:39 ||  20 |          0 |        |      0 |      0 | 2018-10-23 11:05:52 || 100 |          0 |        |      0 |      0 | 2018-10-23 11:05:38 || 101 |          0 |        |      0 |      0 | 2018-10-23 11:06:09 |+-----+------------+--------+--------+--------+---------------------+18 rows in set (0.00 sec)

查找所有重复类型的记录:

SELECT * FROM redis_num_mem a WHERE ((SELECT COUNT(*) FROM redis_num_mem WHERE type = a.type) > 1) ORDER BY type DESC;

一、查找重复记录

1.1  查找全部重复记录

Select * From redis_num_mem Where type In (Select type From redis_num_mem Group By type Having Count(*)>1) ;

1.2  过滤重复记录(只显示一条)

select * From redis_num_mem where id in (Select Max(id) From  redis_num_mem Group By type);

注:此处显示ID最大一条记录

二、删除重复记录

2.1  删除全部重复记录(慎用)

delete from redis_num_mem where type in (select type from (select type from redis_num_mem group by type having count(*)>1)  as t1);

2.2  保留一条(这个应该是大多数人所需要的 ^_^)

delete from redis_num_mem where id not in (select * from (select max(id) from redis_num_mem group by type) as t1);

三、补充

1、查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断

select * from people where peopleId in (select peopleId from people group by peopleId having count(peopleId) > 1)

 

2、删除表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断,只留有rowid最小的记录

delete from people where peopleId in (select peopleId from people group by peopleId having count(peopleId) > 1) and rowid not in (select min(rowid) from people group by peopleId having count(peopleId )>1)

 

3、查找表中多余的重复记录(多个字段)

select * from vitae a where (a.peopleId,a.seq) in (select peopleId,seq from vitae group by peopleId,seq having count(*) > 1)

 

4、删除表中多余的重复记录(多个字段),只留有rowid最小的记录

delete from vitae a where (a.peopleId,a.seq) in (select peopleId,seq from vitae group by peopleId,seq having count(*) > 1) and rowid not in (select min(rowid) from vitae group by peopleId,seq having count(*)>1)

 

5、查找表中多余的重复记录(多个字段),不包含rowid最小的记录

select * from vitae a where (a.peopleId,a.seq) in (select peopleId,seq from vitae group by peopleId,seq having count(*) > 1) and rowid not in (select min(rowid) from vitae group by peopleId,seq having count(*)>1)

 

转载于:https://my.oschina.net/u/3023401/blog/2721842

你可能感兴趣的文章
100万个数中找出最大的前K个数
查看>>
arrayList 和hashSet的区别
查看>>
shell脚本自动修改IP信息
查看>>
【Python进阶】03、json
查看>>
Bitnami-Redmine迁移升级后若干问题解决方案
查看>>
php.ini详细参数讲解
查看>>
Linux内核学习之三内核编程语言与环境
查看>>
初识 XSS 1
查看>>
[C#进阶系列]专题一:深入解析深拷贝和浅拷贝
查看>>
nginx error_log 错误日志配置说明
查看>>
真话和假话:要学着彼此混搭
查看>>
Ruby-条件判断
查看>>
JavaScript思维导图之<函数基础>
查看>>
Java程序员应该知道的10个eclipse调试技巧
查看>>
Office 2007无法安装,提示“不支持从预发布版的 2007 Microsoft Office system 升级
查看>>
ubuntu下切换GDM, LightDM , KDM
查看>>
linux下Apache安装(转)
查看>>
android socket编程实例
查看>>
从火力发电厂到算法研究员
查看>>
域控制器简易备份还原教程
查看>>