select count(*) -- counts the number of items
from
(
select sum(p1.purchasePrice) over (order by p1.purchasePrice asc) as c_price -- Our cumulative price
from product p1
where colour in ('white', 'red') -- limit the colours
) x2
where x2.c_price <= 20 -- where the cumulative is less than the budget
编辑:似乎你是在寻找每件商品中你能买多少,而不是从列表中选择多少:
select colour, purchasePrice,
floor(20/purchasePrice) as qty_for_20 -- floor rounds the number down
from products
where colour in ('white', 'red')
1条答案
按热度按时间cgfeq70w1#
对于mysql 8.0以后的版本,有一些窗口函数可以为允许的颜色创建累计价格:
编辑:似乎你是在寻找每件商品中你能买多少,而不是从列表中选择多少: