datoshway Posted August 3, 2010 Share Posted August 3, 2010 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! Quote Link to comment https://forums.phpfreaks.com/topic/209667-join-statement/ Share on other sites More sharing options...
bh Posted August 3, 2010 Share Posted August 3, 2010 Google helps you. Quote Link to comment https://forums.phpfreaks.com/topic/209667-join-statement/#findComment-1094591 Share on other sites More sharing options...
datoshway Posted August 3, 2010 Author Share Posted August 3, 2010 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 />"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/209667-join-statement/#findComment-1094606 Share on other sites More sharing options...
brianlange Posted August 3, 2010 Share Posted August 3, 2010 Can you post your database structure? Does strTypeName really join to intCategoryID? Quote Link to comment https://forums.phpfreaks.com/topic/209667-join-statement/#findComment-1094625 Share on other sites More sharing options...
datoshway Posted August 3, 2010 Author Share Posted August 3, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/209667-join-statement/#findComment-1094629 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.