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)

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章