Solved

looping one query through another

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

895 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now