Jump to content

Find Duplicate Rows in MySQL


suttercain

Recommended Posts

Hi everyone,

 

I have a table that contains about 18,000 records. The table has a primary key (unique id) and 21 columns.

 

Is there an easy way to locate duplicate rows? I would like to find a rows with the same data, with the exception of the primary key.

 

Thanks.

 

SC

Link to comment
https://forums.phpfreaks.com/topic/100336-find-duplicate-rows-in-mysql/
Share on other sites

So you want to find rows where the 20 columns (all but the primary key) are duplicates?

 

<?php
$fields = array('field1','field2','field3');//list your fields
$sql = "SELECT * FROM users WHERE (`".implode('`,`',$fields)."`) IN(SELECT ".implode('`,`',$fields)." FROM users GROUP BY first,last)";
$result = mysql_query($sql) or die(mysql_error());
//show results
?>

 

If you dont also want to return the primary key, just use the inner query.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.