过去 5 天按天分组
我正在尝试选择过去 5 天的整数字段的总和,并且需要每天对其进行分组。
我在确定分组时遇到了一些问题。到目前为止,这是我的 sql 查询:
select
sum(`amount_sale`) as total
from `sales`
where the_date >= unix_timestamp((CURDATE() - INTERVAL 5 DAY))
它可以很好地生成所有 5 天的总和,但我需要对其进行分解,以便它显示过去 5 天每一天的总和,即:
第 1 天 - 200 美元
第 2 天 - 500 美元
第 3 天 - 20 美元
等
I am trying to select the sum of an integer field for the past 5 days, and I need to group it for each day.
I'm having a bit of issues figuring out the grouping. Here's my sql query so far:
select
sum(`amount_sale`) as total
from `sales`
where the_date >= unix_timestamp((CURDATE() - INTERVAL 5 DAY))
that works fine for generating the sum for all 5 days together, but I need to break this down so that it shows the sum for each of the past 5 days i.e:
day 1 - $200
day 2- $500
day 3 - $20
etc.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
对于缺少的日期返回
0
:这不是一个非常优雅的解决方案,但是,
MySQL
缺乏从头开始生成记录集的方法。To returns
0
for missing dates:This is not a very elegant solution, however,
MySQL
lacks a way to generate recordsets from scratch.使用格式函数返回工作日编号: SELECT DATE_FORMAT(the_date, '%w');
use the format function to return weekday nr: SELECT DATE_FORMAT(the_date, '%w');
使用
在like select * from XXX where date 之间 date(...) 和 date(...) group by date Limit 0,5
应该这样做
use between
like select * from XXX where date between date(...) and date(...) group by date Limit 0,5
should do it