Php how to delete duplicate records from mysql ??


5 Votes, Rating: 3.61 Star2 Stars3 Stars4 Stars5 Stars

Tags: ,

Is there any way to delete duplicate records from mysql by php ???

if you are one who searching this in google, so answer is YES. how i will let you know. see this post below and you will do that easily.

select all database unique results

for select unique records we have to use this query, or group by

 

 
$qry = "select distinct name from tablename";
$db->query( $qry );
while( $data = $db->fetchArray() ) {
 
$qry1 = "select * from tablename where name='".$data['name']."' ";
$db1->query( $qry1 );
if($db1->numRows() > 1) 
 
    { 
$check=0; 
while( $data1 = $db1->fetchArray() ) {
 
 
if($check > 0) 
            {
 
 
####### add delete query here

$sql_del="delete from tablename where Id=".$data['Id']; 
                $db->query( $sql_del ); 
 
 } 
            $check++; 
 
} 
 
}
 
}

 name = the field to check duplicate records

now select again same table with the name, and after that we check all unique records and only delete

records those greater than 1 of number rows.

coz 0 is the original 1st records and we have to keep it and delete similar other records.

Share/Save/Bookmark

3 Responses to “Php how to delete duplicate records from mysql ??”

  1. adijuh.com says:

    Спасибо за коды.

  2. Pankaj Kumar Jha says:

    yes, you can delete duplicate record from mysql using single query..

    ALTER IGNORE TABLE table_name ADD UNIQUE INDEX(col1,col2);

  3. jim-op says:

Leave a Reply