mysql - ORDER BY based on maximum number of row appearence -
table fields: shop_id , product_id
- i want list of shops having specific products(should have @ least 1 product)
- results should sorted on basis of shops having maximum number of specified products
i write sql query 1st part, list not sorted according shops match maximum number of products
select shop_id, product_id products_table product_id in (1,2,3) order ???
is there optimal solution?
join subquery gets counts each shop, , order that.
select a.shop_id, a.product_id products_table join (select shop_id, count(*) product_count products_table product_id in (1, 2, 3) group shop_id) b on a.shop_id = b.shop_id product_id in (1, 2, 3) order b.product_count desc
Comments
Post a Comment