DELETE FROM people WHERE peopleName IN ( SELECT peopleName FROM people GROUP BY peopleName HAVING count(peopleName) > 1 ) AND peopleId NOT IN ( SELECT min(peopleId) FROM people GROUP BY peopleName HAVING count(peopleName) > 1 )
自己使用的时候显示报错:
delete from tb where id in (SELECT max(id) from tb GROUP BY user HAVING count(user)>1)
[Err] 1093 - You can't specify target table ‘XXX' for update in FROM clause
暂时不知道是什么原因导致的。
然后想办法分布操作,首先筛选出有重复user的数据,然后用max()选出其中较大的那一行:
SELECT max(id) from tb GROUP BY user HAVING count(user)>1
然后再根据得到的max(id)逐条删除多余的数据
delete from tb where id=xx
总结
以上所述是小编给大家介绍的MysqL删除重复数据保留最小的id 的解决方法,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。