[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

looping one query through another

Posted on 2009-07-07
2
Medium Priority
?
204 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 39

Accepted Solution

by:
Roger Baklund earned 2000 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

This article discusses how to implement server side field validation and display customized error messages to the client.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

649 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