Ver Mensaje Individual
  #15 (permalink)  
Antiguo 27/07/2011, 13:59
Curda
 
Fecha de Ingreso: septiembre-2009
Ubicación: Queretaro
Mensajes: 51
Antigüedad: 14 años, 7 meses
Puntos: 2
Respuesta: eliminar registros duplicados en mysql

Esta es la solucion que yo utilice juntando MySQL y php y esta solucion junta aun un modificador mas... borra los registros duplicados dentro de grupos dejando solo un registro por grupo.


Código PHP:
ini_set('max_execution_time',0);

print 
"<h2>Borrando duplicados...</h2>";   

$dele 0
$sql mysql_query("SELECT * FROM ".$DBprefix."newsletter GROUP BY grupo ;")or print mysql_error();                       
while (
$row mysql_fetch_array($sql)){
    
$sql2 mysql_query("SELECT * FROM ".$DBprefix."newsletter where grupo = '{$row['grupo']}' GROUP BY address HAVING (COUNT(*) > 1) ;")or print mysql_error();  
        while (
$row2 mysql_fetch_array($sql2)){
            
$sql3 mysql_query("SELECT * FROM ".$DBprefix."newsletter where grupo = '{$row['grupo']}' and address = '{$row2['address']}' ;")or print mysql_error();  
                
$noone 1;
                while (
$row3 mysql_fetch_array($sql3)){    
                    if(
$noone != 1){
                    
$sql4 mysql_query("DELETE FROM ".$DBprefix."newsletter WHERE ID = '{$row3['ID']}';")or print mysql_error();  
                    
$dele++; 
                    }  
                    
$noone++;  
                }
        }
}
print 
$dele." fueron borrados."