首页 > 数据库技术 > 详细

MySQL中删除多余的重复记录

时间:2017-01-28 14:21:16      阅读:229      评论:0      收藏:0      [点我收藏+]

检查重复记录

-- 检查重复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 错误

MySQL中删除多余的重复记录

原文:http://www.cnblogs.com/milton/p/6354229.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!