数据表 sniper_tb 中存在主键 id,字段url,现需要在url字段上添加 unique,但由于url存在重复记录,导致添加失败。

如何删除表中多余的url重复记录,仅保持一条?

思路一

  1. 将 sniper_tb 表按url字段分组,将其中 count(url) > 1 的记录存入一个临时表 tmp中,此临时表同时包含id字段
  2. 将 sniper_tb 表中 url 与 tmp.url 相同的记录找出来设置为集合 tmp2
  3. tmp2.id 不在临时表 tmp.id 中的记录,则为最终需要删除的记录

以上思路的select sql语句如下:

select id from sniper_tb where url in (select tmp.url from ( select url,id from sniper_tb where 1=1 group by url having count(url) > 1) tmp) and id not in (select tmp.id from ( select url,id from sniper_tb where 1=1 group by url having count(url) > 1) tmp)

将其中的 sniper_tbidurl 替换成你本地对应的数据表及字段即可,将最开始的 select id 替换成 delete 即可删除这些多余的重复记录。

以上语句中的 where 1=1 是特意占位出来方便替换查询限制条件的:)

思路二

  1. 将 sniper_tb 表中的记录两两比较,找出 a.url = b.url 的重复记录
  2. 将这些重复记录中的最小 id 存为一个临时集合 tmp
  3. 将 sniper_tb 表中id > tmp.id 的重复记录删除

对应的 select sql 语句如下:

select * from sniper_tb a where id > (select min(id) from sniper_tb b where a.url=b.url)

但在mysql中,直接将 select 替换成 delete语句会出现如下报错:

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'a where id > (select min(id) from sniper_tb b where a.url=b.url)' at line 1

mysql的delete写法有挺多限制,比较好的办法就是先 create 一个临时表,用完之后再drop掉,以上语句的 delete 实现为:

create table tmp as select id from sniper_tb a where id > (select min(id) from sniper_tb b where a.url=b.url);
delete from sniper_tb where id in(select id from tmp);
drop table tmp;

参考资料

删除Mysql数据表中多余的重复记录的sql语句的更多相关文章

  1. SqlServer查找表中多余的重复记录

    1.查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断 select * from people where peopleId in (select peopleId from ...

  2. mysql 删除表中多余的重复记录

    =============================================== 2019/7/16_第1次修改                       ccb_warlock == ...

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

    假如表Users,其中ID为自增长. ID,Name,Sex 1 张三,男 2 张三,男 3 李四,女 4 李四,女 5 王五,男 --查找出最小行号ID的重复记录 select Name,Sex,C ...

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

    delete from Resource where Title in (select Title from Resource group by Title having count(Title) & ...

  5. mysql 数据表中查找、删除重复记录

    为了性能考虑,在阅读之前提醒大家,如果有子查询,子查询查询到的数据最好不要超过总数据量的30%. 查询有重复数据的记录 select * from F group by a,b,c,d having ...

  6. [SQL]查询及删除重复记录的SQL语句

    一:查询及删除重复记录的SQL语句1.查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断select * from peoplewhere peopleId in (select ...

  7. Oracle 查询并删除重复记录的SQL语句

    查询及删除重复记录的SQL语句 1.查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断select * from peoplewhere peopleId in (select  ...

  8. oracle 查询及删除重复记录的SQL语句

    查询及删除重复记录的SQL语句 1.查找表中多余的重复记录,重复记录是根据单个字段(Id)来判断 select * from 表 where Id in (select Id from 表 group ...

  9. 查询及删除重复记录的SQL语句

    1.查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断 select * from people where peopleId in (select peopleId from ...

随机推荐

  1. python学习笔记(5)--迭代器,生成器,装饰器,常用模块,序列化

    生成器 在Python中,一边循环一边计算的机制,称为生成器:generator. 如: >>> g = (x * x for xin range(10)) >>> ...

  2. Andriod学习笔记3:Mac 平台下搭建 CLion 集成开发环境

    1. 安装Xcode 通过App store或者下载安装Xcode. 安装完成之后,最好启动一下Xcode,否则可能会报"Error:The C compiler "/usr/bi ...

  3. C++字符串格式化库:CPPFormatLibrary

    这个是很久之前写的,去年总结了一下,将其单独提取出来,作为一个开源库放到了GitHub上,然而CPPFormat之类的名字都已经被抢注了,结果只好注册了一个这么蛋疼的名字:CPPFormatLibra ...

  4. 数据分析(7):pandas介绍和数据导入和导出

    前言 Numpy Numpy是科学计算的基础包,对数组级的运算支持较好 pandas pandas提供了使我们能够快速便捷地处理结构化数据的大量数据结构和函数.pandas兼具Numpy高性能的数组计 ...

  5. 【Hibernate框架】批量操作Batch总结

    在我们做.net系统的时候,所做的最常见的批量操作就是批量导入.插入.更新.删除等等,以前我们怎么做呢?基本上有以下几种方式: 1.利用循环调用insert方法,一条条插入. public boole ...

  6. Linux学习笔记(7)-进程

    明天开始学习进程,在以前的单片机开发中,都没有进程这个概念,但从网上了解到,这个东西在操作系统中似乎具有很重要的地位,一定好好学习! --------------------------------- ...

  7. Keras官方Example里Mnist-cnn的调试运行

    问题:老板让测试运行Keras官网里的Mnist-cnn.py,结果从下载数据就是一路坑-- 当前环境:Ubuntu12.04.python2.7.Keras 1.1.1(不知道这个版本号对不对,在启 ...

  8. 将excel数据读入matlab

    1.[NUM,TXT,RAW]=xlsread('example'),其中example是你的excel名,假设所有的数据都在example.xls中. 2.NUM返回的是excel中的数据,TXT输 ...

  9. asp.net 页面上的点击事件

    asp.net 页面上 服务器端控件Button 有两个click事件如 <asp:Button ID="Button1" runat="server" ...

  10. travis CI

    travis可对多语言持续继承,本文以nodejs 为例. 首先添加文件.travis.yml 中language: node_jsnode_js:  - "6"  - " ...