[SOLVED] removing duplicate records..

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

waskelton4
Forum Contributor
Posts: 132
Joined: Mon Sep 09, 2002 6:42 pm

Post by waskelton4 »

Using a combination of replies here is what I did....


first used the sql..

Code: Select all

SELECT COUNT(t1.ID) AS siblings FROM table t1 NATURAL LEFT JOIN table t2 GROUP BY t1.ID HAVING siblings > 1
and put the ID field of all the dupes into an array. then used the code..

Code: Select all

$x = 0;
foreach($dupes as $id) {
	$sql = "DELETE FROM table WHERE ID = $id LIMIT 1";
	
	if(mysql_query($sql)) {
		$x++;
	}
}

echo "$x duplicates deleted";
No matter how many duplicates there are this works.. you just have to keep refreshing the page and each time it will remove one of the dupes.

Thanks for your help all!

Will
Post Reply