当前位置: 首页 > news >正文

【删除重复记录】SQL删除重复记录

   1.给name字段修改成唯一索引:

drop  index  idx_name  on  test; alter  table  test  add  unique  index  (name);

这样当向表中添加相同记录的时候,会返回1062的添加失败信息。
但是有一种情况是表中已经有n个重复的记录,这时候我们才想起来要添加唯一索引,再执行上面的操作时,数据库会告诉你已经有重复的记录了,建立索引失败,这时候,我们可以用下面的操作:

alter  ignore  table  test  add unique  idx_name  (name);

它会删除重复的记录(别怕,会保留一条)(但是这个命令在MySQL5.1.37之前是可以的,在5.1.48以后就行不通了),然后建立唯一索引,高效而且人性化。

另外需要注意的是alter ignore table 在percona版本的MySQL行不通,因为它创建索引的方式是:fast index creation

3.删除重复记录法:

创建一个表用来存放,要删除的记录的id信息:

CREATE  TABLE  `tmp_ids` (
`id`  int ( 11 ),
`name`  char( 20)  
) ENGINE = MyISAM;

如果要删除的记录不多的话,可以把这个表创建成内存表形式:

CREATE  TABLE  `tmp_ids` (
`id`  int ( 11 ),
`name`  char( 20
)  ENGINE = HEAP;

然后在test表中删除重复记录:

insert  into  tmp_ids  select  min (id),name  from  test  group by  name  having count ( * ) > 1  order by null; delete  a.*  from  test a,tmp_ids b  where b.name=a.name and  a.id>b.id ; truncate  table  tmp_ids;
摘抄自:https://www.cnblogs.com/sunss/archive/2011/01/29/1947469.html
select * FROM mx_maint_order_list WHERE lIndex NOT IN ( SELECT temp.mid FROM ( SELECT min(lIndex) as mid FROM mx_maint_order_list  GROUP BY lMaintOrderID,lWindFieldID) AS temp);
-- 检查重复code1
select count(identity) num, identity from event_log 
where code='code1' 
group by identity having count(identity) > 1
order by num desc

  

删除重复记录

DELETE FROM event_log WHERE `code`='code1' AND identity IN (
    SELECT identity from (
        SELECT identity FROM event_log WHERE code='code1' GROUP BY identity HAVING count(identity) > 1
    ) a
) AND id NOT IN (
    SELECT keepId FROM (
        SELECT min(id) keepId FROM event_log WHERE code='code1' GROUP BY identity HAVING count(identity) > 1
    ) b
)
 
   

其中 a 和 b 两个中间表的作用是, 避免执行时出现  You can't specify target table 'xxxxx' for update in FROM clause 错误

分组按时间正序取第一条记录, 巧妙地使用了not exists

select d.* from t_charge d where not exists (select 1 from t_charge where user_id = d.user_id and created_at <</span> d.created_at)

  

按时间倒序则是

select f.* from t_charge f where not exists (select 1 from t_charge where user_id = f.user_id and created_at > f.created_at) 

  

 转自:https://www.cnblogs.com/milton/p/6354229.html

在生产环境中,我们有的列是不允许出现重复值的,亦或是某两列不允许同时重复,但由于前端未做限制,或者没限制住,出现了单列重复值,或者两列本应组成唯一组合却也出现重复,这两种情况都是不允许的。现在由于前端应用限制不住,要做删除操作后,添加唯一索引,从数据库层面进行限制,以下是处理过程:

mysql> select * from aixuan1;
+----+------+-------+
| id | text | text1 |
+----+------+-------+
|  1 | aa   | 11    |
|  2 | bb   | 22    |
|  3 | cc   | 33    |
|  4 | cc   | 44    |
|  5 | bb   | 22    |
|  6 | aa   | 11    |
|  7 | dd   | 55    |
+----+------+-------+
7 rows in set (0.00 sec)

text字段全部重复的有:

mysql> select * from aixuan1 where text in (select text from aixuan1 GROUP BY text having count(*) > 1);
+----+------+-------+
| id | text | text1 |
+----+------+-------+
|  1 | aa   | 11    |
|  2 | bb   | 22    |
|  3 | cc   | 33    |
|  4 | cc   | 44    |
|  5 | bb   | 22    |
|  6 | aa   | 11    |
+----+------+-------+
6 rows in set (0.00 sec)

筛选出text单列重复值

select * from aixuan1 where `text` in (select `text` from aixuan1 GROUP BY `text` having count(*) > 1) and id not in (select min(id) from aixuan1 group by text having count(*)>1)
+----+------+-------+
| id | text | text1 |
+----+------+-------+
|  4 | cc   | 44    |
|  5 | bb   | 22    |
|  6 | aa   | 11    |
+----+------+-------+
3 rows in set (0.00 sec)

还可以这么查

mysql> select * FROM aixuan1 WHERE id NOT IN ( SELECT temp.mid FROM ( SELECT min(id) as mid FROM aixuan1 em GROUP BY em.text) AS temp);
+----+------+-------+
| id | text | text1 |
+----+------+-------+
|  4 | cc   | 44    |
|  5 | bb   | 22    |
|  6 | aa   | 11    |
+----+------+-------+
3 rows in set (0.00 sec)

筛选出text和text1同时重复的字段:

mysql> select * FROM aixuan1 WHERE id NOT IN ( SELECT temp.mid FROM ( SELECT min(id) as mid FROM aixuan1 em GROUP BY em.text,em.text1) AS temp);
+----+------+-------+
| id | text | text1 |
+----+------+-------+
|  5 | bb   | 22    |
|  6 | aa   | 11    |
+----+------+-------+
2 rows in set (0.00 sec)

查出来了,删就好办了,把select换成delete就Ok了,具体说保留大的id还是保留小的id那条,只要子查询的id函数用min(id)或者max(id)即可

本文出自 “岁伏” 博客,请务必保留此出处http://suifu.blog.51cto.com/9167728/1796055

相关文章:

  • 【myISAM和innoDB】mySql的引擎myisam和innodb的区别/mysiam
  • 【文档/键值数据库】文档数据库和键值数据库有什么区别
  • 【JDBC与JPA】JDBC与JPA
  • Apache+Tomcat配置转发
  • 【SQLite】SQLServerCompact/SQLiteToolboxVS的SQLite插件使用
  • 【mysql索引】MySQL索引原理及慢查询优化
  • 【趋势和新技术】趋势和技术
  • 【mysql中文编码】mysql中文乱码中文编码问题
  • 【RPC】为什么需要RPC,而不是简单的HTTP接口
  • 【JAVA】java中implements和extends的使用小结
  • 【postgreSQL】Postgres-XL--基于PostgreSQL的开源分布式实现
  • 【python C结构体】Python Ctypes结构体指针处理(函数参数,函数返回)
  • 【慢查询log】MySQL慢查询日志的配置与使用教程
  • 【安装gcc】centOS6、6.5、6.8、6.9……安装/升级gcc方法
  • 【Django工程目录】Django工程目录结构优化
  • JavaScript 如何正确处理 Unicode 编码问题!
  • [NodeJS] 关于Buffer
  • 10个确保微服务与容器安全的最佳实践
  • 230. Kth Smallest Element in a BST
  • 4. 路由到控制器 - Laravel从零开始教程
  • C++回声服务器_9-epoll边缘触发模式版本服务器
  • CSS相对定位
  • ESLint简单操作
  • EventListener原理
  • fetch 从初识到应用
  • Git初体验
  • js 实现textarea输入字数提示
  • open-falcon 开发笔记(一):从零开始搭建虚拟服务器和监测环境
  • 基于web的全景—— Pannellum小试
  • 简析gRPC client 连接管理
  • 如何使用 JavaScript 解析 URL
  • 深入体验bash on windows,在windows上搭建原生的linux开发环境,酷!
  • 消息队列系列二(IOT中消息队列的应用)
  • 一起来学SpringBoot | 第十篇:使用Spring Cache集成Redis
  • 如何通过报表单元格右键控制报表跳转到不同链接地址 ...
  • 完善智慧办公建设,小熊U租获京东数千万元A+轮融资 ...
  • ​【已解决】npm install​卡主不动的情况
  • ​Kaggle X光肺炎检测比赛第二名方案解析 | CVPR 2020 Workshop
  • (day6) 319. 灯泡开关
  • (vue)el-checkbox 实现展示区分 label 和 value(展示值与选中获取值需不同)
  • (附源码)小程序 交通违法举报系统 毕业设计 242045
  • (论文阅读40-45)图像描述1
  • (一)VirtualBox安装增强功能
  • (原創) 如何解决make kernel时『clock skew detected』的warning? (OS) (Linux)
  • (转)IIS6 ASP 0251超过响应缓冲区限制错误的解决方法
  • (转)Linux整合apache和tomcat构建Web服务器
  • (转载)(官方)UE4--图像编程----着色器开发
  • ./mysql.server: 没有那个文件或目录_Linux下安装MySQL出现“ls: /var/lib/mysql/*.pid: 没有那个文件或目录”...
  • .NetCore 如何动态路由
  • .netcore 如何获取系统中所有session_如何把百度推广中获取的线索(基木鱼,电话,百度商桥等)同步到企业微信或者企业CRM等企业营销系统中...
  • ?.的用法
  • @Autowired和@Resource的区别
  • @column注解_MyBatis注解开发 -MyBatis(15)
  • @EnableWebMvc介绍和使用详细demo
  • [ 英语 ] 马斯克抱水槽“入主”推特总部中那句 Let that sink in 到底是什么梗?