Saturday, 29 July 2017

Finding duplicate values in MySQL



I have a table with a varchar column, and I would like to find all the records that have duplicate values in this column. What is the best query I can use to find the duplicates?


Answer



Do a SELECT with a GROUP BY clause. Let's say name is the column you want to find duplicates in:



SELECT name, COUNT(*) c FROM table GROUP BY name HAVING c > 1;



This will return a result with the name value in the first column, and a count of how many times that value appears in the second.


No comments:

Post a Comment

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...