MySQL - need to limit results to 1 from each category?

Hi,
how can I limit the below query to only select one item from each category id?

Thanks

Andrew


SELECT DISTINCT items.id AS id, items.itemName, items.link_id, items.itemDesc, items.itemDesc2, items.itemDesc3, items.itemPrice, items.image1, items.image2, items.image3, items.image4, items.image5, items.image6, items.image7, items.item_type, items.min_order, items.prodcode, items.true_stock_count, items.display_stock_count, items.low_stock_count, items.limit_availability, items.date_time_expires, items.itemDescb, items.multi_price, items.itemPrice, items.retPrice, items.specialPrice, items.specs1, items.spechead1, items.specs2, items.spechead2, items.add_comments, items.more_details, category, items_to_category.position, category.subcat, category.position as cat_order, category.cat_type FROM items_to_category LEFT JOIN category ON category.id = items_to_category.category LEFT JOIN items ON items.id = items_to_category.item WHERE category.id IN ('157','159','160','158') GROUP BY id ORDER BY cat_order, category, position

Open in new window

sabecsAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Walter RitzelSenior Software EngineerCommented:
I think this could be a solution. I took the assumption  that you want the item with the max id for each category. If you have a specific requirement on which item to select from each category, then you can change the query:
SELECT items.id AS id
, items.itemName
, items.link_id
, items.itemDesc
, items.itemDesc2
, items.itemDesc3
, items.itemPrice
, items.image1
, items.image2
, items.image3
, items.image4
, items.image5
, items.image6
, items.image7
, items.item_type
, items.min_order
, items.prodcode
, items.true_stock_count
, items.display_stock_count
, items.low_stock_count
, items.limit_availability
, items.date_time_expires
, items.itemDescb
, items.multi_price
, items.itemPrice
, items.retPrice
, items.specialPrice
, items.specs1
, items.spechead1
, items.specs2
, items.spechead2
, items.add_comments
, items.more_details
, category
, items_to_category.position
, category.subcat
, category.position as cat_order
, category.cat_type 
FROM items_to_category LEFT JOIN category ON category.id = items_to_category.category 
                       LEFT JOIN items ON items.id = items_to_category.item 
WHERE items.id in (select max_id from (select category.id, max(items.id) from  items_to_category LEFT JOIN category ON category.id = items_to_category.category 
                       LEFT JOIN items ON items.id = items_to_category.item  where category.id IN ('157','159','160','158') group by category.id))
ORDER BY cat_order, category, position

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
MySQL Server

From novice to tech pro — start learning today.