in reply to Re: OT: routine for finding duplicate entries from a MySQL Table
in thread OT: routine for finding duplicate entries from a MySQL Table

The idea to add a UNIQUE index is OK, but the way you propose it is not correct.

You are adding a UNIQUE KEY which consists of both the questions field and all the option_? fields, which means that for the same question but with a different set of options, you get a different entry in the index and hence the "NOT UNIQUE" error doesn't trigger.

The solution is therefore to add a UNIQUE KEY on the questions field only and then you can add only one record with this question and all later records with the same question (but with perhaps different options) will not get added to the database.

CountZero

"If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

Replies are listed 'Best First'.
Re: Re: Re: OT: routine for finding duplicate entries from a MySQL Table
by Caron (Friar) on Feb 04, 2004 at 17:18 UTC

    Well, the problem is that the OP wasn't very clear about what is a duplicate record. He mentioned an ID, but it isn't in the table description, so I assumed that a primary key existed, apart the columns that were mentioned.

    Anyway, if the problem is (more trivially) to find out if several records have the same "questions" column, then it's even simpler:

    SELECT questions, COUNT(*) AS howmany FROM yourtable GROUP BY questions HAVING howmany > 1

    To remove such duplicates, on the same vein:

    ALTER IGNORE TABLE yourtable ADD UNIQUE KEY (questions)
      Sorry for the vague description of the problem above. There's actually an AUTO_INCREMENT PRIMARY KEY ques_id that exists in the table, and many other columns that I did not show.

      Your suggestion is highly appreciated. Thanks.