Current location: Hot Scripts Forums » Programming Languages » PHP » delete duplicate rows in mysql using php


delete duplicate rows in mysql using php

Reply
  #1 (permalink)  
Old 01-12-04, 05:20 PM
seans9 seans9 is offline
Newbie Coder
 
Join Date: Sep 2003
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
delete duplicate rows in mysql using php

ok im not that advanced in php...but here is the deal. i have a database with a lot of email addresses (some of which are duplicates). each email address has its own row. what i want to do is go through and delete the rows with duplicate ones (leaving at least one of the email addresses)...i hope that makes sense. this is what i wrote and its not working. PLEASE help. thanks

PHP Code:

$query="SELECT * FROM `emails` ORDER BY `email` ASC LIMIT 0, 30";

mysql_query($query);
$result=mysql_query($query); 

$numrows=mysql_num_rows($result);

$totalemailsdeleted=0;
$i=1;
$i2=0;
while(
$i2 $numrows)
{
    
$id=mysql_result($result$i2id);
    
$email=mysql_result($result$i2email);
    while(
$i $numrows)
    {
        
$idcompare=mysql_result($result$iid);
        
$emailcompare=mysql_result($result$iemail);
        if(
$emailcompare == $email)
        {
            
$query="DELETE FROM `emails` WHERE `id` = '$idcompare' LIMIT 1";
            
mysql_query($query);
            
$emailsdeleted.="$emailcompare<br>";
            
$totalemailsdeleted++;
            
$numrows=mysql_num_rows($result);
        }
        else
        {
        }
        
$i++;
    }
    
$i2++;

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 01-14-04, 01:30 AM
blaw's Avatar
blaw blaw is offline
Junior Code Guru
 
Join Date: Dec 2003
Location: Vancouver, BC, Canada
Posts: 550
Thanks: 0
Thanked 0 Times in 0 Posts
Hi there,

I'm not sure if you've figured out yourself, but for future reference...

ASSUMING that your `emails` table has only one field `email` (besides your PK that you are willing to let go), you can apply DISTINCT keyword and put the results into a new table.

Code:
SELECT DISTINCT email FROM emails;
Run this SQL statement, and you will get unique email addresses. Say, if you have:

Code:
+----+----------------+
| id | email          |
+----+----------------+
|  1 | amy@foo.com    |
|  2 | bob@bar.net    |
|  3 | amy@foo.com    |
|  4 | cathy@hoge.org |
|  5 | bob@bar.net    |
+----+----------------+
Then the above SQL statement with DISTINCT keyword will give you this:

Code:
+----------------+
| email          |
+----------------+
| amy@foo.com    |
| bob@bar.net    |
| cathy@hoge.org |
+----------------+
Then, you can use a loop or something to insert these emails into a new table or generate a CSV file and import it from MySQL.

HTH.
__________________
Blavv =|
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 05-01-08, 11:44 AM
offsound offsound is offline
New Member
 
Join Date: May 2008
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
this would be easier..

You could use phpmyadmin, mysql console or even php..

Backup your table first because i didn't test this, but try these queries:

Code:
CREATE TABLE emails_temp AS SELECT * FROM emails WHERE 1 GROUP BY email;

DROP TABLE emails;

RENAME TABLE emails_temp TO emails;
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #4 (permalink)  
Old 05-01-08, 01:52 PM
amigura's Avatar
amigura amigura is offline
Newbie Coder
 
Join Date: Feb 2008
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Code:
CREATE TABLE emails_temp AS SELECT * FROM emails WHERE 1 GROUP BY email;

DROP TABLE emails;

RENAME TABLE emails_temp TO emails;
be careful in doing this as it will kill all your primary,index, auto increm


this is a rework of wat you were trying to do
Code:
$sql = "SELECT id,count(email) as etot FROM emails group by email having etot > 1"; 
$result = mysql_query( $sql );while( $row = mysql_fetch_array( $result ) ) {

$id.= $row["id"].',';
//$delem = "delete from emails where id = '".$row["id"]."'"; $delquery = mysql_query($delem);
}

$id=substr($id, 0, -1);
$delem = "delete from emails where id in ($id)"; $delquery = mysql_query($delem);
__________________
amigura.co.uk
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #5 (permalink)  
Old 07-16-08, 10:42 PM
iseecom iseecom is offline
New Member
 
Join Date: Jul 2008
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
A way to remove duplicates without recreating tabls

Hi Guys!

Here is my solution ... and I have tested it ... it works ..
PHP Code:

function RemoveDuplicates($TableName$UniqueFieldName$IDFieldName$FirstFoundIDValue)
{
            
$Query =    "DELETE  FROM ".$TableName.
            
                            
" WHERE    ".$IDFieldName." IN ".
                            
                                
"( SELECT    a.".$IDFieldName." FROM ".$TableName." a, ".$TableName." b 
                                
                                        WHERE    (a."
.$UniqueFieldName." = b.".$UniqueFieldName.") AND (a.".$IDFieldName." > ".$FirstFoundIDValue.")

                                        GROUP BY a."
.$IDFieldName."

                                        HAVING COUNT(a."
.$UniqueFieldName.") > 1
                                 )"
;
// Example Use:            
//delete  from tblps_CommEquip where PSID in    ( select a.PSID from tblps_CommEquip a, tblps_CommEquip b     where    (a.CommEquipType = b.CommEquipType) AND (a.PSID > 80)    
//                      group by a.PSID having count(a.CommEquipType) > 1)
            
            
db_query($Query);



Last edited by Nico; 07-17-08 at 03:28 AM. Reason: Please use [php] wrappers when posting PHP code.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Error when trying to create MySQL table via PHP HasansWeb PHP 5 05-19-11 07:59 AM
PHP and MySQL ? rob2132 Hot Scripts Forum Questions, Suggestions and Feedback 4 08-29-08 03:22 AM
Need Epinions-lite system in PHP & MYSQL wali001 Job Offers & Assistance 4 01-12-04 07:02 AM
PHP to MySQL script question...(using a field to update info in MySQL) DisneyFan25863 PHP 4 11-02-03 04:31 AM
select & delete mysql queries superman PHP 2 07-20-03 04:08 AM


All times are GMT -5. The time now is 09:34 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.