1 Replies - 2031 Views - Last Post: 09 January 2015 - 10:20 PM

#1 SpaceMan   User is offline

  • D.I.C Addict
  • member icon

Reputation: 10
  • View blog
  • Posts: 507
  • Joined: 20-February 03

DeDup MySQL records

Posted 13 February 2008 - 09:36 AM

Description: Change settings, upload and run. if you cant create the table with the page due to permissions, make it manually in some admin program.With MySQL you can not update in the same query of returned records. simple work around. in PHP. will delete one occurrence of a duplicate. if 2 or more dups need to add a loop, or rerun till it exits.
db errors " [email protected]_error()." File Line:". __LINE__ ."
rn");
}  
$SQL = '
TRUNCATE TABLE  `'.DB_TEMP_TABLE.'`
';        
  if([email protected]_query($SQL))
    exit($SQL."
rndb errors " [email protected]_error()." File Line:". __LINE__ ."
rn");


$SQL = '
INSERT  INTO `'.DB_TEMP_TABLE.'`
(SELECT MIN( `'.DB_DEDUP_ID_NAME.'` ) `'.DB_DEDUP_ID_NAME.'`
FROM `'.DB_DEDUP_TABLE.'`
GROUP  BY `'.DB_DEDUP_FIELD_NAME.'`
HAVING count(  *  )  >1)
';        
  if([email protected]_query($SQL))
    exit($SQL."
rndb errors " [email protected]_error()." File Line:". __LINE__ ."
rn");

$SQL = '
SELECT count(*) FROM `'.DB_DEDUP_TABLE.'`
';        
  if($c = @mysql_fetch_array(@mysql_query($SQL))){
    if($c[0] < 1) exit('No records in table '.DB_DEDUP_TABLE.' to dedup...');
  } else exit($SQL."
rndb errors " [email protected]_error()." File Line:". __LINE__ ."
rn");

$SQL = '
SELECT count(*) FROM `'.DB_TEMP_TABLE.'`
';        
  if($c = @mysql_fetch_array(@mysql_query($SQL))){
    if($c[0] < 1) exit('No dups found...');
  } else exit($SQL."
rndb errors " [email protected]_error()." File Line:". __LINE__ ."
rn");
 
$SQL = '
DELETE FROM `'.DB_DEDUP_TABLE.'` WHERE `'.DB_DEDUP_ID_NAME.'` IN ( SELECT id FROM `'.DB_TEMP_TABLE.'`)
';        
  if([email protected]_query($SQL))
    exit($SQL."
rndb errors " [email protected]_error()." File Line:". __LINE__ ."
rn");

echo 'records deleted = ' . @mysql_affected_rows();

$SQL = '
TRUNCATE TABLE  `'.DB_TEMP_TABLE.'`
';        
  if([email protected]_query($SQL))
    exit($SQL."
rndb errors " [email protected]_error()." File Line:". __LINE__ ."
rn");


?>


Is This A Good Question/Topic? 0
  • +

Replies To: DeDup MySQL records

#2 macosxnerd101   User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12510
  • View blog
  • Posts: 45,644
  • Joined: 27-December 08

Re: DeDup MySQL records

Posted 09 January 2015 - 10:20 PM

Note: The mysql_*() family of functions is deprecated. Please refer to MySQLi or PDO, using prepared statements in particular. I will leave this snippet as the concept is still valid and the mysql_*() functions were valid back in 2008, when this snippet was submitted.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1