Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/facebook/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MySQL查询:找到数量所在的范围,得到该范围内最大数量的价格_Mysql - Fatal编程技术网

MySQL查询:找到数量所在的范围,得到该范围内最大数量的价格

MySQL查询:找到数量所在的范围,得到该范围内最大数量的价格,mysql,Mysql,在生成查询时需要帮助。我有数量价格表,列出数量和相应的价格如下所示 Quantity Price ---------------- 1 -- € 175,35 2.5 -- € 160,65 5 -- € 149,10 10 -- € 143,85 数量价格 ---------------- 1 -- € 175,35 2.5 -- € 160,65 5 -- € 149,10 10 -- € 143,85 因此,最多1个数量的价格为175,35,最多2.5

在生成查询时需要帮助。我有数量价格表,列出数量和相应的价格如下所示

Quantity Price ---------------- 1 -- € 175,35 2.5 -- € 160,65 5 -- € 149,10 10 -- € 143,85 数量价格 ---------------- 1 -- € 175,35 2.5 -- € 160,65 5 -- € 149,10 10 -- € 143,85 因此,最多1个数量的价格为175,35,最多2.5个数量的价格为160,65,以此类推。对于超过10个数量,价格将保持在143,85


现在,如果我的数量是1.5,那么查询应该返回价格160,65,这意味着查找数量所在的范围,然后获取该范围内最大数量的价格。

使用
where
语句查找大于1.5的所有行;然后使用
limit
orderby
获取数量最少的行。正如Petah所评论的,总是包含数量最大的行是很方便的。例如:

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

你想用MySQL还是PHP来做这件事?那么你为什么要把这个问题标记为PHP如果数量超过10,那么这个问题就失败了?这不应该是按数量排序的ASC?这不应该是
=myquantity
)。。。如果
myquantity
超过10,它就不起作用了?
select price
from quantity_price
where myquantity >= quantity
order by quantity
limit 1