Find SQL records which have identical values in two fields
Posted by sarneil on 04 Nov 2011 in Activity Log
I had to find all instances of criminals in which both the given-name and surname fields are identical and the number of such duplicates
Here's the SQL I used for the example:
SELECT `surname`, `given_name`, COUNT(`surname`) AS Instances FROM criminals GROUP BY `surname`, `given_name` HAVING ( COUNT(`surname`) > 1 ) AND ( COUNT(`given_name`) > 1)
and here's the same SQL with abstracted table and field names:
SELECT `field1Name`, `field2Name`, COUNT(`field1Name`) AS Instances FROM tableName GROUP BY `field1Name`, `field2Name` HAVING ( COUNT(`field1Name`) > 1 ) AND ( COUNT(`field2Name`) > 1)