scottybwoy Posted September 17, 2008 Share Posted September 17, 2008 Is it possible to have a query within a query? Let me expand... I have a table of categories and a table of products that belong to the categories. In the table of categories there may be child categories. Usually the parent category doesn't have products in it, but just child categories within the same table. The structure is like this to keep it simple I will just show the relevant fields : | categories_id | parent_id | ===================== | 29 | | | 30 | 29 | | 31 | 30 | | 32 | 30 | ... And the other table is just as simple : | product_id | category_id | ===================== | 21 | 31 | | 22 | 32 | | 23 | 31 | ... So is it possible to have a single query that can take a category_id, lets say 29 in this example, discover whether it is a parent, if so does its child have a daughter (this loop recursive until the category has no children) Then retrieve all the product_ids for it? Hope that makes sense? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/124627-queries-within-queries/ Share on other sites More sharing options...
scottybwoy Posted September 19, 2008 Author Share Posted September 19, 2008 Bump, is it too complex? Not possible? How do I go about finding out, any good resources? Quote Link to comment https://forums.phpfreaks.com/topic/124627-queries-within-queries/#findComment-645475 Share on other sites More sharing options...
Aimee Posted September 19, 2008 Share Posted September 19, 2008 Can you not do a recursive function instead? I imagine it would be more flexible. So in my example I'm just returning a list of IDs but you get the point.. function getChildren(parentID) { $sql = "SELECT id FROM categories WHERE parentID='$parentID'"; // loop through results and for each one call: $allChildren .= "$id,"; $allChildren .= getChildren($id); return $allChildren; } $allChildren = getChildren(""); Quote Link to comment https://forums.phpfreaks.com/topic/124627-queries-within-queries/#findComment-645512 Share on other sites More sharing options...
scottybwoy Posted September 19, 2008 Author Share Posted September 19, 2008 Yes, that is what I have set up so far, but as it gets bigger there would be calls to the database and I thought it would be much quicker if it could be done in a single MySQL query. Thanks for the reply tho. Quote Link to comment https://forums.phpfreaks.com/topic/124627-queries-within-queries/#findComment-645524 Share on other sites More sharing options...
aschk Posted September 19, 2008 Share Posted September 19, 2008 You can do it in 1 statement but it requires an alteration to your table layout. The other option is to perform some LEFT JOINs although i haven't fully considered this option yet regarding sister categories... Quote Link to comment https://forums.phpfreaks.com/topic/124627-queries-within-queries/#findComment-645604 Share on other sites More sharing options...
scottybwoy Posted September 19, 2008 Author Share Posted September 19, 2008 OK so there is no fandagaley recursive MySQL functions that could do it? Quote Link to comment https://forums.phpfreaks.com/topic/124627-queries-within-queries/#findComment-645647 Share on other sites More sharing options...
fenway Posted September 29, 2008 Share Posted September 29, 2008 No, not without a stored procedure... Quote Link to comment https://forums.phpfreaks.com/topic/124627-queries-within-queries/#findComment-653271 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.