store with categories of goods
it is necessary for each category (sorted by popularity) to select one product (sorted by popularity), but since products can be repeated from category to category, it is necessary to make a selection of products unique.
There are three tables:
categories:
cats id,name,popularity products:
items id,name,popularity relationship table:
cats2items cat_id, item_id at the output you need to get like:
cat_id item_id 1 5 3 1 2 56 10 177 Zillion options tried, here's the last one:
SELECT cats2items.cat_id, i.item_id FROM cats2items inner join ( select items.id, items.popularity FROM items ORDER by items.popularity DESC ) i WHERE cats2items.item_id = i.id AND cats2items.cat_id IN (SELECT cats.id FROM cats ORDER BY cats.popularity DESC) GROUP BY cats2items.cat_id but the products are not unique and they are not sorted by popularity:
cat_id item_id 1 86 2 3 3 1 4 49 5 3 tell me where to dig pliz