View Full Version : Find all duplicates in a mysql field

08-18-2009, 09:43 PM
If you are ever in need of finding duplicates in a mysql field but dont want to open phpmyadmin you can simply do this.

mysql_connect("localhost", "username", "password") or die(mysql_error());
mysql_select_db("dbname") or die(mysql_error());

$result = mysql_query("select field, count(field) as cnt
from table
group by field
having cnt > 1
order by cnt; ")
or die(mysql_error());

while($row = mysql_fetch_array( $result ))

echo $row['field']."<br>";

This will return any duplicates, but only 1 result of the dup.

Of course change table and field in the snippet to your own.