Jump to content

Easy way of deleting duplicates in mysql


brown2005

Recommended Posts

Well if you're using a script to have it be inserted into the database you could do.

 

$sql = "SELECT * FROM `table` WHERE `field1` ='$field1'";
$res = mysql_query($sql) or die(mysql_error());
if(mysql_num_rows($res) > 0){
//dont insert
}else {
//insert query
}

Define "easy"...

 

DELETE 
FROM table_a 
WHERE id in 
(
                       SELECT id
                       FROM table_a 
                       GROUP BY column_a 
                       HAVING COUNT(*) > 1
)

 

Best,

 

Patrick

SQL query: 

 

DELETE FROM tablea WHERE email IN (

 

SELECT email

FROM tablea

GROUP BY email

HAVING COUNT( * ) >1

)

 

MySQL said: 

 

#1064 - You have an error in your SQL syntax.  Check the manual that corresponds to your MySQL server version for the right syntax to use near 'SELECT email

FROM tablea

GROUP BY email

HAVING COUNT(*) >

 

i get that mate

Yes, that's what I need.  Furthermore let me explain something before I wipe out your data.  The SQL I gave you will delete all duplicates in the literal sense.  That is, any email which appears more than once will be wiped out.  So if the email [email protected] appeared twice, after running the query all records containing that email address would be deleted.  I don't think, looking at your query, that is your intention.  It seems that you want to delete all subsequent duplicate email addresses, preserving the original.  Let me know if this sounds right.

 

Patrick

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.