« Ben On Restful Web Applications And Memcached | Main | Furry PHP Mascot »

Tips : Howto Find MySQL Duplicates

It happen that we need to do some manual manipulations in MySQL databases, to clean data and fix things. One of my databases here have a username field which is not unique, and so full of duplicates. Don't ask me who created this db, it's something that exist!. A quick solution to retrieve duplicates is the following :

SELECT MAX(ID) as ID,USERNAME,Count(*) as c FROM `TABLE` group by USERNAME having c>1 order by ID asc

If the ID is using autoincrement, the MAX() will help to get the new values that have to be removed, or kept.

Bookmark this article at these sites
Comments
1

cool, thanks for this. it was just the solution i was looking for.

Post a comment





(Email will remain hidden)





Please enter the security code you see here




Related entries
Email to a friend
Email this article to:


Your email address:


Message (optional):