isedeasy Posted June 2, 2010 Share Posted June 2, 2010 I have a table that contains products, each product has a category. I would like to display each category on the page and have 10 products under each heading. Is this possible to do with 1 query, I know how to group my results by category but how would I limit each group? Quote Link to comment https://forums.phpfreaks.com/topic/203655-limit-within-groups/ Share on other sites More sharing options...
isedeasy Posted June 2, 2010 Author Share Posted June 2, 2010 Actually I'm not sure that grouping will help me at all... Is there away without doing a query per category? Quote Link to comment https://forums.phpfreaks.com/topic/203655-limit-within-groups/#findComment-1066777 Share on other sites More sharing options...
isedeasy Posted June 2, 2010 Author Share Posted June 2, 2010 Would I need to use subqueries? There must be away of doing it with out me creating a query per each category (~30)... Quote Link to comment https://forums.phpfreaks.com/topic/203655-limit-within-groups/#findComment-1066866 Share on other sites More sharing options...
isedeasy Posted June 3, 2010 Author Share Posted June 3, 2010 Ok let me try and explain this better. At the moment I have 30 category's, I am doing 30 SELECT queries with a LIMIT 5 (one for each category). Is there a way to make this more efficient ie by somehow combining it into 1 query. I have read about subqueries but I am yet to use one and not sure if its possible/effective for what I want. Quote Link to comment https://forums.phpfreaks.com/topic/203655-limit-within-groups/#findComment-1067264 Share on other sites More sharing options...
ignace Posted June 3, 2010 Share Posted June 3, 2010 SELECT * FROM categories AS c, (SELECT * FROM products WHERE category_id = c.id LIMIT 10) AS p WHERE c.id = p.category_id Something like this? Quote Link to comment https://forums.phpfreaks.com/topic/203655-limit-within-groups/#findComment-1067321 Share on other sites More sharing options...
fenway Posted June 4, 2010 Share Posted June 4, 2010 You can't combine LIMIT and JOIN -- you'll have to figure out which 10 you want first. Quote Link to comment https://forums.phpfreaks.com/topic/203655-limit-within-groups/#findComment-1067951 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.