MySQL query: find how much the range is in, and then get the price of the maximum quantity in the range

Need help building a query. I have a table of quantities, prices and the corresponding price, as shown below

Quantity price
----------------
1 - € 175.35
2.5 - € 160,65
5 - € 149.10
10 - € 143.85

thus, for quantities up to 1, the quantity will be from 175.35 to 2.5, it will be 160.65, and so on. For more than 10 quantities, the price will remain up to 143.85.

Now, if my quantity is 1.5, the request should return the price of 160.65, that is, find how much the range is in, and then get the price of the maximum quantity in the range.

+5
source share
2 answers

where, 1,5; limit order by, . , . :

select  *
from    quantity_price
where   Quantity > 1.5
        or Quantity = (select max(Quantity) from quantity_price)
order by
        Quantity
limit   1
+1
select price
from quantity_price
where myquantity >= quantity
order by quantity
limit 1
+1

All Articles