php - Mysql data selecting with particular price range, product limit, and other filtering -
i have database table product example:
id name price category 1 pen 70 1 2 banana 50 1 3 potato 10 1 5 apple 20 1 6 guaba 30 1 7 papa 30 2 8 salt 80 2 9 butter 25 2 10 cheese 10 2 11 burger 20 2 12 mashala 25 2 13 chuijal 40 3 14 icecream 20 3 15 chocolate 35 3
i want product mysql database based on price range, product limit, , category based. example: when want 4 products category=1 on cumulative price of 4 products 100 or near 100, show 4 product category 1 , price of 4 products 100 in total. possible output:
id name price category 1 pen 10 1 2 banana 50 1 3 potato 10 1 5 apple 20 1 ============================ =90 (near 100)
again when want 3 products of category 1, prices cumulative 100, show 3 product category 1 prices cumulative 100 or near 100.
possible out put:
id name price category 1 pen 70 1 3 potato 10 1 5 apple 20 1 ============================ =100
thus way, want result form different category different price range , product limit. data example, actual data many items , many different prices , other filterings category filtering.
i appreciate experts!
you can write separate query each problem products:
select p1.id, p2.id, p3.id, p4.id products p1 join products p2 on p1.id < p2.id , p1.category = 1 , p2.category = 1 join products p3 on p2.id < p3.id , p3.category = 1 join products p4 on p3.id < p4.id , p3.category = 1 order abs(100 - (p1.price + p2.price + p3.price + p4.price)) limit 1;
this doing brute force search, generating combinations of products. should work find on data, if have thousands of rows, might take while.
optimizing type of search not problem can solve wholly within database. can review wikipedia page on knapsack problem better understand it. section on computational complexity explains why brute force approach best known approach optimization (in case don't understand computational complexity).
Comments
Post a Comment