Posted by on 12/07/2014
Query for Finding Duplicates in an SQL TableMost of the time you'll encounter cases wherein you'll need to extract the entries on your database for duplicates. In that case, here is a query.

SELECT * FROM [TABLE_NAME] 
GROUP BY [COLUMN_TO_CHECK] HAVING (COUNT([COLUMN_TO_CHECK]) > 1)

Assumed that you have a table called users and you'll extract items with same the same email address (if for some weird reason that you didn't include UNIQUE attribute on your columns). The code would go like this:

SELECT * FROM `users` GROUP BY `email` HAVING (COUNT(`email`) > 1)

This code would also work the other way around in case you want to search for records that occur exactly once:

SELECT * FROM `users` GROUP BY `email` HAVING (COUNT(`email`) = 1)

No comments :

Post a Comment