MySQL 删除重复记录
==========
A really easy way to do this is to add a UNIQUE index on the 3 columns. When you write the ALTER statement, include the IGNORE keyword. Like so:
ALTER IGNORE TABLE jobs ADD UNIQUE INDEX idx_name (site_id, title, company );
This will drop all the duplicate rows. As an added benefit, future INSERTs that are duplicates will error out. As always, you may want to take a backup before running something like this...
==========
Another possible solution that I've just come across:
DELETE n1 FROM names n1, names n2 WHERE n1.id > n2.id AND n1.name = n2.name
if you want to keep the row with the lowest id value OR
DELETE n1 FROM names n1, names n2 WHERE n1.id < n2.id AND n1.name = n2.name
if you want to keep the row with the highest id value.
I used this method in MySQL 5.1
Not sure about other versions.
*NB - You need to do this first on a test copy of your table!+ When I did it, I found that unless I also included AND n1.id <> n2.id, it deleted every row in the table.
==========
Add Unique Index on your table:
ALTER IGNORE TABLE `TableA`
ADD UNIQUE INDEX (`member_id`, `quiz_num`, `question_num`, `answer_num`);
OR
Add primry key in your table then you can easily remove duplicates from your table using below query:
DELETE FROM member
WHERE id IN (SELECT *
FROM (SELECT id FROM member
GROUP BY member_id, quiz_num, question_num, answer_num HAVING (COUNT(*) > 1)
) AS A
);
==========
REF:
http://stackoverflow.com/questions/3311903/remove-duplicate-rows-in-mysql
http://stackoverflow.com/questions/4685173/delete-all-duplicate-rows-except-for-one-in-mysql
MySQL 删除重复记录的更多相关文章
- mysql 删除重复记录语句
mysql 根据条件删除重复记录 只保留最小id的重复数据 DELETEFROM newsWHERE news_id IN ( SELECT a.news_id FROM ( SELECT news_ ...
- Mysql删除重复记录,保留id最小的一条
mysql 查询重复字段,及删除重复记录的方法MySQL, 数据库, 数据库, 字段, 服务器数据库中有个大表,需要查找其中的名字有重复的记录id,以便比较.如果仅仅是查找数据库中name不重复的字段 ...
- mysql删除重复记录语句的方法
例如: id name value 1 a pp 2 a pp 3 b iii 4 b pp 5 b pp 6 c pp 7 c pp 8 c iii id是主键 要求得到这样的结果 id name ...
- MySQL删除重复记录只保留一条
删除表中重复记录,只保留一条: delete from 表名 where 字段ID in (select * from (select max(字段ID) from 表名 group by 重复的字段 ...
- mysql删除重复记录,只保留最大ID的记录(非重复也保留)
目前网上搜索的删除重复记录,大部分都是where子查询,本人感觉看上去不美观,故亲自手写了一个,如下: delete from mst_sku using mst_sku,( select dist ...
- MySQL删除重复记录的方法
参考网上的方法,总结了产出重复记录的方法,欢迎交流. 参考:http://www.cnblogs.com/nzbbody/p/4470638.html 方法1:创建一个新表临时储存数据 假设我们有一个 ...
- mysql删除重复记录语句,删除除了 id 号不同,其他都相同的学生冗余信息
/** 在Mysql下执行: delete from my.stu where id not in( select min(id) id from my.stu group by code) ; 用途 ...
- mysql删除重复记录,保存Id最小的一条
方法1:1.创建一个临时表,选取需要的数据.2.清空原表.3.临时表数据导入到原表.4.删除临时表.mysql> select * from student;+----+------+| ID ...
- Mysql 删除重复记录,只保留最小的一条
delete from `jb_postcontent` where id not in(select min(id) from (select * from `jb_postcontent`) as ...
随机推荐
- 自定义DataSet
//创建数据集 DataSet dataSet = new DataSet(); //创建虚拟数据表 DataTable datatable = new DataTable(); //获取列集合,添加 ...
- MetaClass
它的作用主要是 指定由谁来创建类,默认是type #python3 class Foo(metaclass=MyType): pass #python2 class Foo(object): __me ...
- 剪花布条---hdu2087(kmp模板)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2087 kmp模板题: #include <cstdio> #include <cst ...
- SSL延迟有多大?(转)
add by zhj: SSL层在TCP层之上,SSL握手是在TCP握手完成之后,除了这点之外,两者应该是相对独立的过程.在服务端,这两个过程有可能不在同一台主机上, 比如服务端用LVS+Nginx实 ...
- MySQL优化(二):SQL优化
一.SQL优化 1.优化SQL一般步骤 1.1 查看SQL执行频率 SHOW STATUS LIKE 'Com_%'; Com_select:执行SELECT操作的次数,一次查询累加1.其他类似 以下 ...
- python包管理一防丢失
pip3 freeze >list.txt 导出当前环境安装的所有包(list是当前项目录下的文件,可以自己命名)pip3 install -r list.txt 安装文件中所 ...
- Ignite缓存管理初体验
Ignite缓存管理初体验:ignite服务端配置,大家可以用参考官方进行配置(或者使用默认配置也可以). 本文中的ignite使用版本是1.7,与spring结合使用.maven依赖配置 ignit ...
- 学习Zookeeper需要了解的专业名词
一.Zookeeper的集群角色 Leader:该角色是整个zookeeper集群工作机制中的核心 Follower:该角色是zookeeper集群状态的跟随者 Observer:在集群中充当观察者的 ...
- docker——镜像(image)
镜像相关命令一览表: docker images docker tag docker inspect docker history docker search docker pull/push doc ...
- xlrd xlwt操作
简介 xlrd,xlwt和xlutils是用Python处理Excel文档(*.xls)的高效率工具.其中,xlrd只能读取xls,xlwt只能新建xls(不可以修改),xlutils能将xlrd.B ...