数据表 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. Codeigniter基础

    一.去除index.php 新建.htaccess: RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUES ...

  2. Cocos2d-x 版本小游戏 《是男人就下100层》 项目开源

    这个是很久就开始动手写的一个小游戏了,直到最近才把它收尾了,拖拖拉拉的毛病总是很难改啊. 项目是基于 cocos2d-x v2.2 版本 ,目前只编译到了 Win8 平台上,并且已经上传到了商店,支持 ...

  3. maven权威指南学习笔记(五)—— POM

    1. 简介 Archetype插件通过 pom.xml 文件创建了一个项目.这就是项目对象模型 (POM),一个项目的声明性描述. 当Maven运行一个目标的时候,每个目标都会访问定 义在项目POM里 ...

  4. linux命令二

    4. df -h 查看磁盘使用情况    cpu:        user,system,idle,iowait    mem:        total,used,free,cached,buffe ...

  5. Entity FrameWork 5 增删改查 & 直接调用sql语句

    class="brush:csharp;gutter:true;"> #region 1.0 新增 -void Add() /// <summary> /// 1 ...

  6. ShowMessageFmt 用法

    举例:ShowMessageFmt('n1:%x; n2:%x', [n1,n2]);  显示Params 为 array[integer] of TvarRec 进制表示 %o(8)   print ...

  7. winform下的简易播放器

    编写这个播放器,遇到很多问题,比如目前只实现了wav音频文件的播放,而对于这个图中中间所标注的按钮 不能实现让其暂停的功能,同时当点击的时候,让其文本变为"▷",对于这部分功能不知 ...

  8. Redis 主从配置

    环境     Master/Slave     系统 IP Redis版本 Master     CentOS6.7         10.10.3.211         redis-3.2.6   ...

  9. Leetcode Implement Queue using Stacks

    Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...

  10. 基于Unity有限状态机框架

    这个框架是Unity wiki上的框架.网址:http://wiki.unity3d.com/index.php/Finite_State_Machine 这就相当于是“模板”吧,自己写的代码,写啥都 ...