Jump to content

Recommended Posts

So i'm taking over a big database for a client.  I really need to make a unique index on the table, but I can't because there are already tons of duplicates from bad design in the past.

 

What is a quick way to kill all these duplicates so I can set the unique index?

Link to comment
https://forums.phpfreaks.com/topic/230570-set-unique-indices-but-duplicate-rows/
Share on other sites

Firstly BACKUP the original db.

 

I would create a new table with an id field with Auto Increment set then use something like this: 

 

SELECT distinct email FROM originaltable WHERE id > 0

 

Get the results of that query and then paste them into this.

 

INSERT INTO newtable SET email = value1, name = value2.....then any other fields.

 

Hope this helps.

  • 2 weeks later...

I wrote this a while back to check a single field in a table for duplicate entries. You should be able to use it or modify it to at least build the list of duplicates.

 

<?php
function mysqli_check_duplicates($db_conn, $table, $field, $flag = 1) {
    $field = $field;
    $table = $table;
    require_once("$db_conn");
    $output = '';
    $query = "SELECT `{$field}`, COUNT(`{$field}`) AS c FROM `{$table}` GROUP BY `{$field}` ORDER BY c DESC";
    $result = mysqli_query( $dbc, $query );
    $output = "<table>";

    while( $array = mysqli_fetch_assoc($result) ) {
        if( $array['c'] > $flag ) {
            $bg != "CCCCCC" ? $bg = "CCCCCC" : $bg = "FFFFFF";
            $count = $array['c'];
            $output .=  "<tr bgcolor=\"$bg\"><td>Value:</td><td>" . $array[$field] . "</td><td>Entries:</td><td>" . $count . "</td></tr>";
        }
    }
    $output .= "</table>";
    return $output;
}

// mysqli_check_duplicates('db_conn_with_path', 'table_name', 'field_name', $flag); // $flag - 0 = show all rows, 1 = (default) only show duplicates
?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.