mysql根据一个字段保留最新删除其他的

在mysql中,如用户a插入了多个数据,保留最新的即id最大的值,删除其他的数据

delete from `user_nicks`
where `bianhao` in(select a.bianhao from (select `bianhao` from user_nicks group by bianhao having count(bianhao) > 1) a)
and id not in(select b.id from (select max(id) as id from user_nicks group by bianhao having count(bianhao )>1) b)

 

你可能感兴趣的:(PHP编程)