You've got two different ways of doing this (I'm going to use the SQL I know, from Oracle, but the concepts the same, if MySQL doesn't support the same syntax):
which simply delete all records which have a higher key value, but are identical on fields 1 & 2 (extend the list as necessary). How efficient this'll be depends on your database. But all the information is there, so it could be your best bet.DELETE FROM source dup WHERE EXISTS ( SELECT NULL FROM source WHERE dup.field1=source.field1 AND dup.field2=source.field2 AND source.key<dup.key)
The other option, using perl is along the lines of:
Disclaimer: I've just made this up, so I may have got the specifics wrong, but hopefully you get the general idea: keep pulling the records out, in order of the fields you want to match, and when you find a correspondance, kill the duplicate.my $data=$dbh->prepare("SELECT key, field1, field2 FROM source ORDER B +Y field1, field2"); my $del=$dbh->prepare("DELETE FROM source WHERE key=?"); my @last; while (my @record=$data->fetchrow_array()) { if ($record[1] eq $last[1] && $record[2] eq $last[2]) { $del->execute($record[0]); } @last=@record; } $data->finish(); $del->finish();
--
Tommy
Too stupid to live.
Too stubborn to die.
In reply to Re: Removing duplicates in MySQL
by tommyw
in thread Removing duplicates in MySQL
by shaolin_gungfu
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |