SirChick Posted November 12, 2007 Share Posted November 12, 2007 I am wondering if something like this can be done: SELECT userid from userstable Then check a table named Hospital... for userid Then what i need to do is a while loop which will "only" while loop through users that are not in the hospital table but are in the user table..... meaning only people out of hospital will be queried but "all" out of hospital users must be ran through the while loop.. is this possible in one query? Link to comment https://forums.phpfreaks.com/topic/77036-comparing-2-tables/ Share on other sites More sharing options...
Psycho Posted November 12, 2007 Share Posted November 12, 2007 You only need 1 query. Using a loop for queries is extremely inefficient and can create performance issues. The following query should return all users that exist in the user table which do not have any associated records in the hospital table. SELECT u.* FROM userstable u LEFT JOIN hospitals h ON u.userid = h.userid WHERE h.AnyColumInHospitalTable = NULL Link to comment https://forums.phpfreaks.com/topic/77036-comparing-2-tables/#findComment-390111 Share on other sites More sharing options...
SirChick Posted November 12, 2007 Author Share Posted November 12, 2007 I tried this : $Get = mysql_query("SELECT u.* FROM userregistration u LEFT JOIN hospital h ON u.UserID = h.UserID WHERE h.IllnessType = NULL") or die (mysql_error()); while(mysql_fetch_assoc($Get)){ echo 'test'; } but it didn't work =/ It should have done 4 echo's for 'test' Link to comment https://forums.phpfreaks.com/topic/77036-comparing-2-tables/#findComment-390161 Share on other sites More sharing options...
Branden Wagner Posted November 12, 2007 Share Posted November 12, 2007 What did it do? did you get an error? Link to comment https://forums.phpfreaks.com/topic/77036-comparing-2-tables/#findComment-390179 Share on other sites More sharing options...
SirChick Posted November 12, 2007 Author Share Posted November 12, 2007 no it didnt echo basically.... just a blank screen Link to comment https://forums.phpfreaks.com/topic/77036-comparing-2-tables/#findComment-390231 Share on other sites More sharing options...
Psycho Posted November 12, 2007 Share Posted November 12, 2007 OK, try changing the "= NULL" to "IS NULL". I have tested it and it works for me. Link to comment https://forums.phpfreaks.com/topic/77036-comparing-2-tables/#findComment-390274 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.