ccompass Posted May 19, 2011 Share Posted May 19, 2011 I have three tables: events, orderdetails & orders. First I query orderdetails to find all the records that match the EventID: $query1 = SELECT * FROM orderdetails WHERE EventID = $_SESSION['EventID']; This returns 4 records. These 4 records have a field called DetailOrderID which is the foreign key for orders.OrderID. Next I need to query the results of the first query to find all the records in the orders table that match up. For example: SELECT * from orders where $query1.DetailOrderID = orders.OrderID. How would I go about doing this? I'm head down the temporary table solution but wanted to through this one out for discussion before I invest too much time. Quote Link to comment https://forums.phpfreaks.com/topic/236909-querying-table-results/ Share on other sites More sharing options...
mikosiko Posted May 19, 2011 Share Posted May 19, 2011 and what about a simple JOIN between the tables? Quote Link to comment https://forums.phpfreaks.com/topic/236909-querying-table-results/#findComment-1217816 Share on other sites More sharing options...
jcbones Posted May 19, 2011 Share Posted May 19, 2011 What is your full table structure? I'll attempt to get something working for you without it, but this query may not work. SELECT a.*,b.* FROM orderdetails AS a JOIN orders AS b ON a.DetailOrderID = b.OrderID MySQL table JOIN syntax Quote Link to comment https://forums.phpfreaks.com/topic/236909-querying-table-results/#findComment-1217818 Share on other sites More sharing options...
ccompass Posted May 19, 2011 Author Share Posted May 19, 2011 Here are the two tables: orderdetails ------------------------- DetailID (PK) DetailOrderID DetailProductID EventID DetailName DetailPrice DetailQuantity orders ------------------------ OrderID (PK) OrderAmount OrderShipName OrderShipAddress OrderCity OrderState OrderZip OrderPhone OrderEmail OrderDate This code seems to work, but I don't know how to query the $result array. $result = mysql_query("SELECT * FROM orderdetails WHERE EventID =" . $_GET['EventID']) or die(mysql_error()); while($row = mysql_fetch_array($result)){ echo "DetailID: " . $row['DetailID'] . " | DetailOrderID: " . $row['DetailOrderID'] . "<br>"; } Quote Link to comment https://forums.phpfreaks.com/topic/236909-querying-table-results/#findComment-1217825 Share on other sites More sharing options...
jcbones Posted May 19, 2011 Share Posted May 19, 2011 Did you try my query? Quote Link to comment https://forums.phpfreaks.com/topic/236909-querying-table-results/#findComment-1217827 Share on other sites More sharing options...
ccompass Posted May 19, 2011 Author Share Posted May 19, 2011 I did not. Pardon my ignorance but what do the 'a' and 'b' mean? :-\ Quote Link to comment https://forums.phpfreaks.com/topic/236909-querying-table-results/#findComment-1217843 Share on other sites More sharing options...
jcbones Posted May 20, 2011 Share Posted May 20, 2011 It is just table aliases, so I didn't have to type the names out every time. Quote Link to comment https://forums.phpfreaks.com/topic/236909-querying-table-results/#findComment-1217861 Share on other sites More sharing options...
ccompass Posted May 24, 2011 Author Share Posted May 24, 2011 Figured it out: SELECT * FROM orderdetails, orders WHERE orderdetails.EventID = ".$_SESSION['EventID']." AND orderdetails.DetailOrderID = orders.OrderID Thanks all! Quote Link to comment https://forums.phpfreaks.com/topic/236909-querying-table-results/#findComment-1219801 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.