The post title is really a lot longer and more confusing than how to fix the problem. The solution was a lot simpler than I thought it would be. If you read my last post about how to import a CSV file and insert the records into the MySQL db, you’ll know what I’m talking about. That last post I put out there as I thought it might be useful for someone looking for a quick way to import CSV records into a MySQL db.
LIke the post before that on how to fill in a form via cURL and post it, and this below information on how to check for dupes now that you have done that CSV import, I hope that someone finds them useful bits of information to help them build something really cool.
Anyway, once you have imported your CSV file into your MySQL db, you don’t want duplicate records. Well most people don’t. If that is the case, like most situations that call for a CSV import, here is a solution that is pretty easy. I was surprised it actually was this easy.
Basically you create another table by doing a select via group by, email is the field I used, but depending upon your table, you may need to do it for another field that is unique to the data you are storing. Next you drop the original table and do the alter table to recreate by renaming the the table you created to the original table. One key thing, is that when you create your table you must create any keys you already have and make sure you do it via order by.
$db->query("CREATE table contacts2 (ID INT AUTO_INCREMENT PRIMARY KEY) SELECT * from contacts GROUP BY(Email) order by ID"); $db->query("DROP TABLE contacts"); $db->query("ALTER TABLE contacts2 RENAME TO contacts");
I put that in right after the last script finished up right after the line that said – fclose($handle); . That was it. Pretty simple to clean up dupes in a table and keep the table clean on a CSV import. I hope that someone finds this post useful.