Link to home
Start Free TrialLog in
Avatar of Bruce Gust
Bruce GustFlag for United States of America

asked on

How can I identify and delete these duplicates?

I've got a table that looks like this

id    contestant_id   voter_id   contest_end_date   ballots

In some instances, I'm getting entries like this:

1      12345          0000          2012-09-30   1
2      12345          0000          2012-09-30   1
3      67890          1111          2012-09-30   4

Entry #3 is gold, but the first two represent duplicates that I need to delete. So, in light of the fact that the duplicates are defined in terms of two columns rather than just one, how do I identify them and then how do I delete them in the context of a loop / delete dynamic?
Avatar of Gary
Gary
Flag of Ireland image

Are you saying delete all rows that contain duplicates and not even keep one row?
What are the two columns that need to be duplicates?
SOLUTION
Avatar of dimmergeek
dimmergeek
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Do you want to delete all of the duplicates or all but one of the duplicates?

To access two columns in the array, you would probably want to concatenate the data elements.
Avatar of Bruce Gust

ASKER

All but one of the duplicates would be ideal.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Don't think you can get simpler than this, though it does create a new table.

CREATE TABLE new_table as
SELECT * FROM mytable WHERE 1 GROUP BY concat(contestant_id,voter_id);

Don't run on live data until you've verified it is working correctly
Running the first section of my original post will give you the records that have duplicates, and you can use that to determine which ones to delete.
@GaryC123: Since your script at ID: 38378088 creates a new table, there is no real risk of running it on live data ;-)
doh lmao

but better safe than sorry....
Roger that!
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I got it! Thanks for the feedback!