Solved

looping one query through another

Posted on 2009-07-07
2
198 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
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…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

808 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