mysql對表中數據根據某一字段去重


要刪除重復的記錄,就要先查出重復的記錄,這個很容易做到

select * from 表名 group by 根據哪一個字段(簡稱字段) having count(字段) > 1

注意:這是查出所有重復記錄的第一條記錄,需要保留,因此需要添加查詢條件,查出所有的重復記錄

select id,expect from cqssc where expect in (select expect from cqssc group by expect having count(expect)>1) 

and id not in(select min(id) from cqssc group by expect having count(expect)>1)

 

然后

delete from cqssc where id in (select id from (select id from cqssc where expect in 

(select expect from cqssc group by expect having count(expect)>1) and id not in

(select min(id) from cqssc group by expect having count(expect)>1)) as tmpresult)

刪除成功,最后再查詢一下看是否還有重復記錄

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM