Published on 2007-06-18 09:52:42

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.


Related Entries

Member of the PHP Magazine Network, Copyright (C) 2005-2009 phpmagazine.net All Rights Reserved