Jump to content

Completing a Join Statement


datoshway

Recommended Posts

I need help!  Banging my head against the wall on this all day.  I really need some assistance on finishing this statement heres the summery.  I'm using mysql version 5.2.6

 

I have a table set up with 3 fields in it

 

intID

intItemID

intIconID

 

This database is correlating to the intID of another database. So my statement needs to detect what Id we are on and show only those rows with that unique ID.  Here is what I have so far.

queryGetIcons	= db_query($strQuery);

		if (db_num_rows($queryGetIcons) > 0) {
			$objRow			= db_fetch_object($queryGetIcons);
			$strHeadline	= stripslashes($objRow->strIconName);
			db_free_result($queryGetIcons);

			$strPostURL		= "Icon/" . $intIconID;

			$strQuery		= sprintf(	"SELECT i.intID, i.strName FROM tblItems i LEFT JOIN tblItemToIcon c ON c.intItemID = i.intID WHERE c.intIconID = %d ORDER BY i.strName ASC", 
										intval($intIconID));
		} else {
			header("Location: " . STR_RELATIVE_PATH);
		}

 

Link to comment
Share on other sites

Sorry I had pasted in a older version of my code.  This is the most recent that I can't seem to get working. 

 

$queryGetIcons	= db_query($strQuery);

		if (db_num_rows($queryGetIcons) > 0) {
			$strFilename	= stripslashes($objRow->strFilename);
			$txtIcon	= stripslashes($objRow->txtIcon);

			db_free_result($queryGetIcons);

			$strQuery		= sprintf(	"SELECT i.intID, i.strName FROM tblItems i LEFT JOIN tblItemToIcon c ON c.intItemID = i.intID WHERE c.intIconID = %d ORDER BY i.strName ASC", 
										intval($intIconID));
		} else {
			header("Location: " . STR_RELATIVE_PATH);
		}

Link to comment
Share on other sites

Three tables are at hand here

 

tblIcons

tblItems

tblItemToIcon

 

The tbl Items is the table holding all the product data.  tblIcons is the table holding all the Icon data.  And tblItemToIcon is the table holding the associations to each product.  That table looks like this

 

intID intItemID i intIconID

305  27            4

 

The intIconID is the ID for the Icons in tblIcons, and the intItemID is ID for the product in tblItems.

 

What we need to happen here is when the user is on ItemID it should check those rows to see if the IconID exists for the ItemID

 

 

Link to comment
Share on other sites

Hi

 

It should be fine.

 

Try the following line in your code

 

$strQuery = sprintf("SELECT i.intID, i.strName, c.intIconID FROM tblItems i LEFT OUTER JOIN tblItemToIcon c ON c.intItemID = i.intID WHERE c.intIconID = %d ORDER BY i.strName ASC",intval($intIconID));

 

It is doing a LEFT OUTER JOIN, so wil bring back the records from the tblItems and if they exist those from tbItemToIcon.

 

If there isn't a matching icon then intIconID will come back as null

 

All the best

 

Keith

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.