首页 > 代码库 > MySQL中删除多余的重复记录
MySQL中删除多余的重复记录
检查重复记录
-- 检查重复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中删除多余的重复记录
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。