Remove duplicate entries from php mysql database
To remove duplicate entries from php mysql database based on a fieldname you have to use some combination of sql queries. The idea is to select all ids of unique emails and then delete the rest apart from those ids. Here is a working code snippet
The first query is used to select ids of the row with GROUP BY clause. The second query deletes all other rows apart from those collected ids with NOT IN clause.
The first query is used to select ids of the row with GROUP BY clause. The second query deletes all other rows apart from those collected ids with NOT IN clause.
$ids = array(); $query = "SELECT id FROM emails GROUP BY email"; $result = mysql_query($query); while($row = mysql_fetch_assoc($result)){ $ids[] = $row['id']; } mysql_query("DELETE FROM emails WHERE id NOT IN (".implode(",", $ids).")");
Remove duplicate entries from php mysql database
Reviewed by JS Pixels
on
July 16, 2011
Rating:
thank you so much for this code!!!
ReplyDeleteI am a very new beginner to php and my programmer simply disappeared leaving me helpless with a duplicate sql fields bug...
thanks to you I have fixed this !
Great Blog, Thank you so much for the code. God Bless.
ReplyDeleteThanks you so much!
ReplyDeleteIt's save my much time.
Great solution. It has solved all my problems.
ReplyDeleteThanks a lot.