在sequelize ORM中计算开始日期和结束日期之间每天的记录数(在nodejs中执行此查询)
我将 start_time 和 end_time 作为查询参数传递到 API 中,并希望获得每天从 start_time 到 end_time 的记录计数。 它为我提供了指定开始时间和结束时间之间的记录总数,但我想单独计算每一天的记录数。 示例 start_time:2017-10-07,end_time 为 2017-10-10 所以,我想计算日期的记录数:2017-10-07、2017-10-08、2017-10-09 和 2017-10-10。
I am passing start_time and end_time as a query parameter in an API and want to have count of the records on each day from start_time and end_time.
It is giving me total count of records between the specified start and end_time but I want count for each day individually.
Example start_time: 2017-10-07 and end_time is 2017-10-10
So, I want count for the number of records on the date: 2017-10-07, 2017-10-08, 2017-10-09 and 2017-10-10.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您希望在 sql 中使用
GROUP BY
子句来按天对计数进行分组。以下是有关如何使用 Sequelize 执行此操作的文档。https://sequelize.org/master/manual/model-querying- basics.html#grouping
如果您的日期不只是一天,而是有小时和分钟,您可能需要在
组中进行一些
。以下是一些相关信息:日期截断
通过按日期进行 Sequelize 分组,忽略小时/分钟/秒
You want a
GROUP BY
clause in your sql to group your count by days. Here's documentation on how to do that with Sequelize.https://sequelize.org/master/manual/model-querying-basics.html#grouping
And if your date isn't just a single day, but has hours and minutes, you might need to do some
date truncation
in yourgroup by
. Here's some info on that:Sequelize grouping by date, disregarding hours/minutes/seconds