Jump to content

comparing 2 tables


SirChick

Recommended Posts

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

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

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

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.