Solved

looping one query through another

Posted on 2009-07-07
2
199 Views
Last Modified: 2012-05-07
I have 3 doctors: DrID 1, 2 and 3. Each Doctor gets their own list of Types. The TypeIDs and the DrIDs are brought together in a table: TypesDrs like this:

TypeDrID  DrID  TypeID
1              1        5

TypeDrID  DrID  TypeID
1              2        5

TypeDrID  DrID  TypeID
1              3        5

I need to generate 2 lists on one page : first list is the Types assigned to the DrID and the other is the Types NOT assigned to the DrID.

here's the example:

http://pacificcenterplasticsurgery.com/admin/add_procedure2.php?DrID=3

    * Breast Lift (Mastopexy) [Add]  [Delete]
    * Breast Reduction [Add]  [Delete]

Are assigned to DrID=3. I'm trying to generate a list below that that will exclude the Types that are not assigned to DrID=3. This is tricky because if I say:

WHERE TypesDrs.DrID <>".$drid." I still get the Types I'm trying to exclude because they are assigned to the other 2 doctors.

/*TYPES TO DELETE (THE TYPES CURRENLTY LISTED UNDER THE DOCTOR*/
mysql_select_db($database_pacific, $pacific);
$query_rsTypes = "SELECT Type, TypesDrs.TypeID, TypesDrs.DrID FROM Types, TypesDrs WHERE DrID=".$drid." AND Types.TypeID=TypesDrs.TypeID ORDER BY Type ASC";
$rsTypes = mysql_query($query_rsTypes, $pacific) or die(mysql_error());
$row_rsTypes = mysql_fetch_assoc($rsTypes);
$totalRows_rsTypes = mysql_num_rows($rsTypes);
 
/*TYPES TO ADD (THE TYPES NOT CURRENLTY LISTED UNDER THE DOCTOR*/
mysql_select_db($database_pacific, $pacific);
$query_rsTypesAdd = "SELECT distinct Type, TypesDrs.TypeID, TypesDrs.DrID FROM Types, TypesDrs WHERE DrID<>".$drid." AND Types.TypeID=TypesDrs.TypeID ORDER BY Type ASC";
$rsTypesAdd = mysql_query($query_rsTypesAdd, $pacific) or die(mysql_error());
$row_rsTypesAdd = mysql_fetch_assoc($rsTypesAdd);
$totalRows_rsTypesAdd = mysql_num_rows($rsTypesAdd);

Open in new window

0
Comment
Question by:phillystyle123
2 Comments
 
LVL 39

Accepted Solution

by:
Roger Baklund earned 500 total points
ID: 24798495
This type of problem can be solved with a LEFT JOIN query. This type of query returns NULL when a row in the joined table is not found. Testing for NULL in the WHERE clause returns the types NOT related to this doctor.
SELECT Types.Type, Types.TypeID
FROM Types
LEFT JOIN TypesDrs ON 
  TypesDrs.DrID = $drid AND 
  TypesDrs.TypeID = Types.TypeID
WHERE TypesDrs.TypeID IS NULL
ORDER BY Types.Type ASC

Open in new window

0
 

Author Closing Comment

by:phillystyle123
ID: 31600792
hot damn! that completely did the trick - thanks so much for the info!!!!!
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question