Solved

looping one query through another

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
The viewer will learn how to dynamically set the form action using jQuery.
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.

705 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

15 Experts available now in Live!

Get 1:1 Help Now