在MySql中删除重复项

时间:2016-06-09 21:06:55

标签: mysql

我继承了一个包含"重复问题"。

的数据库

我跑的时候:

SELECT myFIELD,COUNT(*)c from myTABLE GROUP by myFIELD HAVING c> 1;

我得到了600条重复的记录

无或三倍或任何其他多重

我希望杀掉其中一条记录,让我没有重复。

查询是什么样的?

1 个答案:

答案 0 :(得分:1)

您可以使用这样的查询...您应该根据同一行重复的次数重复查询。

delete from my_table 
where (myFIELD, id) in  
(select a.myFIELD, max(a.id) 
FROM myTABLE as a  GROUP BY a.myFIELD HAVING count(*) > 1)

否则你可以使用

delete from my_table 
where (myFIELD, id) not in 
(select a.myFIELD, min(a.id) 
FROM myTABLE as a GROUP BY a.myFIELD )

这应删除镜头中的所有重复行

如果表名有问题

delete from my_table 
where (myFIELD, id) in  (select field, id from 
(select a.myFIELD as field, max(a.id) as  id
FROM myTABLE as a  GROUP BY a.myFIELD HAVING count(*) > 1) as t)