To get a list of duplicate records, you can join the table on itself. Something like this ought to do it:

SELECT a.unique_id FROM my_table as a, my_table as b WHERE a.field1 = b.field1 and a.field2 = b.field2 and a.field3 = b.field3 and a.unique_id != b.unique_id
This will return a list of all unique_id's where two rows share the same value of field1, field2, and field3. Note that it will return the unique_id of Both matching rows, not just one of them.

I don't have a suitable dataset to experiment on, but you might be able to also do a group by the fields which must match, and select max(unique_id) instead, to grab one row ID from each group of matching stuff. You might still need to use perl to loop through and delete results, but at least you'll then be doing a linear operaton instead of exponential in perl.

Update: changed unique_id to a.unique_id

You can group by a.unique_id to make sure you only get one row per unique id. However, in the case where you know that a large portion of your rows have duplicates, you're right: this still doesn't give you any information you don't already have Slightly better would be select a.unique_id, b.unique_id so you at least know which row each row matches. Still less than ideal, I agree.

Alan


In reply to Re: Removing duplicates in MySQL by ferrency
in thread Removing duplicates in MySQL by shaolin_gungfu

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.