Jump to content

JOIN statement


datoshway

Recommended Posts

I need some help.. I have two separate database tables.  What I need to do is display the data from one if a variable exists in another.  Heres my statement that pulls the correct data from one.  But how do I attach the other so it's only pulling the data from the tables if the ID number is a certain one. 

 

	$strQuery				= "SELECT intID, strTypeName FROM tblTypes WHERE boolActive = 1 ORDER BY intSortOrder ASC, strTypeName ASC";
	$queryGetTypes			= db_query($strQuery);

 

The table I need to join has the following fields: intID, intTypeID, intCategoryID

I need to pull the data for the intCategoryID=1 for example. 

 

Please help!

 

Link to comment
https://forums.phpfreaks.com/topic/209667-join-statement/
Share on other sites

Thanks... So I think i'm pretty close, i'v seemed to make the join statement, just need a little help on completing this full statement. I got this but it's pulling the first field and repeating it instead of listing all the ones with that ID

 

<?php
// Make a MySQL Connection
// Construct our join query
$query = "SELECT 
    * 
FROM 
     tblTypes
JOIN 
    tblTypeToCategory 
ON 
   strTypeName=intCategoryID WHERE intCategoryID = 1 ORDER BY intSortOrder ASC, strTypeName ASC";

$result = mysql_query($query) or die(mysql_error());


// Print out the contents of each row into a table 
while($row = mysql_fetch_array($result)){
echo $row['strTypeName']. " - ". $row['intCategoryID'];
echo "<br />";
}
?>

Link to comment
https://forums.phpfreaks.com/topic/209667-join-statement/#findComment-1094606
Share on other sites

Well now that i'm looking at this might need to join 3 to get these results.  Here is the structure

 

tblTypes

intID  strTypeName

 

tblTypeToCategory

intID  intTypeID  intCategoryID

 

tblCategories

intID  strCategoryName 

 

Ideally what I need is tblTypes to pull all the names that are associated with tblCategories specific ID

 

So for example. 

tblTypes has Item 1, Item 2, Item 3

 

tblTypeToCategory has the category ID's and Type ID

 

tblCategories has the main ID that we are looking to pull so the TypeToCategory is basically the bridge between the two

Link to comment
https://forums.phpfreaks.com/topic/209667-join-statement/#findComment-1094629
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.