gpt4 book ai didi

php - MySQL 中的 SUM 与 ORDER BY 和 LIMIT

转载 作者:行者123 更新时间:2023-11-29 03:34:40 28 4
gpt4 key购买 nike

假设我有以下 MySQL 表:

enter image description here

我想知道最近销售日期前 3 天富士苹果的总销量(如移动总量)。因此,对于这个示例,我选择的是 2014 年 4 月 1 日之前 3 天的总成交量,即 9。

我做了很多次尝试,但都没有达到预期的结果:

SELECT sum(volume) FROM (SELECT `volume` FROM `fruit_sale_db` WHERE `fruit` = 'apple' AND `type` = 'fuji') AS subquery ORDER BY `date` DESC LIMIT 1,3

我认为 ORDER BY date DESC LIMIT 1,3 可以通过将日期限制为 3 从倒数第二个条目开始工作,但它不起作用。

最佳答案

SELECT sum(volume) FROM `fruit_sale_db` where 'date' >= (latest_sale_date - 3) and 'date' <= latest_sale_date and `fruit` = 'apple' AND `type` = 'fuji' 

latest_sale_date 就像

SELECT `date` FROM `fruit_sale_db` WHERE `fruit` = 'apple' AND `type` = 'fuji' ORDER BY `date` DESC LIMIT 1

关于php - MySQL 中的 SUM 与 ORDER BY 和 LIMIT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24348095/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com