mysql——查詢重復數據,及刪除重復數據只保留一條數據


查詢 text 表中,user_name字段值重復的數據及重復次數

select user_name,count(*) as count from text 
group by user_name having count>1

 

刪除 text 表中,重復出現的數據只保留 ID 最大的一條數據,沒有重復的數據不刪除。

delete from text where DW_DATE=20171227 AND
 id not in( select id from (select max(id) as id,count(user_name) as count from text
WHERE DW_DATE=20171227
group by user_name having count =1 order by count desc) as tab)
AND
id not in( select id from (select max(id) as id,count(user_name) as count from text
WHERE DW_DATE=20171227
group by user_name having count >1 order by count desc) as tab)

 


免責聲明!

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



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