widget Posted June 21, 2007 Share Posted June 21, 2007 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 https://forums.phpfreaks.com/topic/56537-comparing-2-tables-and-deleting-information-if-not-found/ Share on other sites More sharing options...
aniesh82 Posted June 21, 2007 Share Posted June 21, 2007 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 https://forums.phpfreaks.com/topic/56537-comparing-2-tables-and-deleting-information-if-not-found/#findComment-279305 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.