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
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
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
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.