0

I have some rows in a MySQL database which are duplicates, except for their ID number.

For example, I might have this as two rows:

ID   | wordlist | category
845  | abashed  | confused
1800 | abashed  | confused

I want to find all of them, and only leave one.

Can I use SQL to do this, or do I need to write a program of some sort?

To clarify, I only want to get duplicates in the wordlist section.

Steven Matthews
  • 9,705
  • 45
  • 126
  • 232
  • if they are referenced in another table that would leave your database incorrect as you won't know which ID was referenced for a particular repeating word – codingbiz Jul 28 '12 at 16:49

2 Answers2

1

You can do this with an outer join in the DELETE statement:

DELETE a FROM tbl a
LEFT JOIN
(
    SELECT MIN(id) AS id
    FROM tbl
    GROUP BY wordlist
) b ON a.id = b.id
WHERE a.category = '<category here>' AND b.id IS NULL

This will eliminate duplicates (in the wordlist column) for only a specific category and keeps the one with the lowest id.

Zane Bien
  • 22,685
  • 6
  • 45
  • 57
0
ALTER IGNORE TABLE your_table ADD UNIQUE INDEX idx_wordlist (wordlist );
Sourav
  • 17,065
  • 35
  • 101
  • 159