Jump to content

Comparing 2 tables and deleting information if not found???


widget

Recommended Posts

Hi, my sites using mysql and php.

 

I have 2 tables I wish to compare.

 

members2 ( which holds the members data such as username password email address etc etc )

 

members_profile2 ( which holds the users profile information and a few other bits and bobs )

 

What I would like to do is delete all profile information for users who no longer exist in the members2 table.

 

Below is the code I have written, yes im a newb so I have no idea if this is how you do it.

I dont want to test it and risk wiping out my users - omg what a nightmare that would be.

 

$members_check1 = mysql_query("SELECT * FROM `members2` ORDER BY `members2`.`username`");

$members_check2 = mysql_query("SELECT * FROM `members_profiles2` ORDER BY `members2`.`username`");

if($members_check2[username] != $members_check1[username])
{
mysql_query("DELETE FROM `members2` WHERE `members2`.`username` = $members_check1[username]");
}
?>

 

Any help id much appreciated

 

 

I assumes that both table have common id ..if not delete the record from second table.

 

select id  from members_profile2  where id NOT IN ( select id from  member2)

/* so get id's you need to delete */

 

use mysql_query and extract the ids . the use the delete query

 

delete from members_profile2  where id IN (/* use ids retrieved from the above 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.