Mysql Select rows to the sum of

Viewed 2154

I have a table with the following info:


id |      Item ID     | stock_package_id | amount | price
0  |        775       |         1        |   16   |  0.22
1  |        758       |         2        |   10   |  0.28
2  |        775       |         3        |   10   |  0.30
3  |        774       |         4        |   10   |  0.25
4  |        775       |         5        |   10   |  0.10
5  |        775       |         6        |   10   |  0.55


My issue is i'm trying to create a query (simple as possible as this table will be left joined with a few other tables) that will allow me to enter an amount:

e.g.

I want 22 of Item_Id 775 selecting from the cheapest price first.

So what I would want returned is:


id |      Item ID     | stock_package_id | amount | price
4  |        775       |         5        |   10   |  0.10
0  |        775       |         1        |   12   |  0.22 - this is only 12 as we only want 22 total

a pesudo example:

select from stock_table until the sum of amount is equal to or greater than input number (22) order by price

Is this possible to do with MySql?

2 Answers
Related