I'm trying to delete all duplicates from my database. I found the code below on this website. Problem is it deleted my entire database. Sadly I don't know enough about MySQL to tell why it got rid of all entries and not just the duplicates.
DELETE FROM RosterPool开发者_如何学JAVA AS t1
WHERE EXISTS (SELECT 1 from RosterPool t2
WHERE t1.Rate = t2.Rate
AND t1.FullName = t2.FullName
AND t1.Last4 = t2.Last4
AND t1.Graduated = t2.Graduated);
Is there a way to move all the duplicates to another table and just delete that table?
There's no such thing as a "move" operation in SQL databases. There's insert, update, and delete only.
If you want to play it safe, you can turn the delete query into a select query to see exactly WHAT will be nuked if you run it as a delete query, but there's no way to "move" a row from one table to another, without doing a insert/select + delete operation sequence.
just force a uniqe key on the column you want to remove duplicates from. to add a unique key for column rate you write:
ALTER TABLE RosterPool ADD UNIQUE (Rate);
that going to give you an error if you already have duplicate rows, but by adding the keyword IGNORE, you can force it to add the uniqe and ignore the warnings
ALTER IGNORE TABLE RosterPool ADD UNIQUE (Rate);
精彩评论