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

Link to comment
Share on other sites

 

 

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 */ )

Link to comment
Share on other sites

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.