How to find duplicated values in MySQL (or/and probably any other RDBM)

As an example I take one real (almost) table I have in my db.

Structure is very simple :

Table name: “words”, field “filename” name of the file and “words” just some information about each file.
+—————–+———————+
|   filename     |     words         |
+—————–+———————+

Suddenly we had a few files with exactly same name and I could find them, so… SQL here to help!

SELECT count( * ) AS num, filename
FROM words
GROUP BY filename
HAVING count( * ) > 1

Simple and quick explanation: we group output by “filename” and count output, if output amount more then 1, display it, together with filename.

PS: why we had duplicated fields? We had two fields with NULL values inside. Stupid import from MS Access…

Posted on February 16, 2009 at 14:55 by admin · Permalink
In: English, sql · Tagged with: , ,

Leave a Reply