1

我有一张类似于下表的表格。我试图找出今天所有价格的总和。

| id| price |       created        |
|---|-------|----------------------|
| 0 |  500  | 2018-04-02 11:40:48  |
| 1 | 2000  | 2018-04-02 11:40:48  |
| 2 | 4000  | 2018-07-02 11:40:48  |

下面的代码是我想出的,但它似乎不起作用。

const TODAY = new Date();
const SUM = await OrdersModel.sum('price', {
    where: {
      created: TODAY,
    },
});
console.log(SUM);

即使今天有条目,SUM 的值也为 0。我也尝试了以下方法,但也没有用。

const TODAY = new Date();
const SUM = await OrdersModel.sum('price', {
    where: {
      created: Sequelize.DATE(TODAY),
    },
});
console.log(SUM);

终端查询的SQL语句如下。

执行(默认): SELECT sum(`price`) AS `sum` FROM `orders` AS `orders` WHERE `orders`.`created` = '2019-05-27 18:30:00';

4

5 回答 5

12

这里发生的是您正在比较确切的时间戳,例如'2019-05-27 11:40:48'equal to '2019-05-27 18:30:00'。所以这个比较永远不会给你结果,因为即使是同一天(5 月 27 日),但时间不同。

所以在这里你有一个可能的解决方案。

const Op = Sequelize.Op;
const TODAY_START = new Date().setHours(0, 0, 0, 0);
const NOW = new Date();

const SUM = await OrdersModel.sum('price', {
    where: {
      created: { 
        [Op.gt]: TODAY_START,
        [Op.lt]: NOW
      },
    },
 });
 console.log(SUM);

您需要创建这样的查询:created < [NOW] AND created > [TODAY_START] 为什么?因为你会得到之后注册的所有价格的总和NOW。此代码还将帮助您获得一系列日期的总数。

PostgreSQL 的替代品

请注意,PostgreSQL 允许您截断到特定的时间间隔。因此,您可以调用该sequelize.fn()方法来创建一个调用“date_trunc”的查询,您可以在此链接中阅读更多信息。像这样:

const SUM = await OrdersModel.sum('price', {
    where: {
      sequelize.fn('CURRENT_DATE'): {
        [Op.eq]:  sequelize.fn('date_trunc', 'day', sequelize.col('created'))
      }
    },
});
console.log(SUM);

还记得更新到最新版本

npm i sequelize@5.8.6 --s
于 2019-05-28T10:41:54.017 回答
0

添加 DATE FUNCTION 进行日期比较而不考虑时间

const TODAY = new Date();
const SUM = await OrdersModel.sum('price', {
    where: {
      sequelize.fn('CURRENT_DATE'): {$eq:  sequelize.fn('date_trunc', 'day', sequelize.col('created'))}
    },
});
console.log(SUM);
于 2019-05-28T10:45:19.070 回答
0

使用时刻会更容易

const moment = require('moment');
const Op = require('sequelize').Op;
const SUM = await OrdersModel.sum('price', {
    where : {
                created_at : { [Op.gt] : moment().format('YYYY-MM-DD 00:00')},
                created_at : { [Op.lte] : moment().format('YYYY-MM-DD 23:59')}
            },
});
console.log(SUM);
于 2020-12-16T16:52:20.607 回答
0

您可以使用Sequelize.literal

    const { Op } = Sequelize;
    const options = {
        where: {}
    };

    options[Op.and] = [
        sequelize.where(Sequelize.literal('DATE(created) = CURDATE()'))            
    ] 

    const SUM = await OrdersModel.sum('price', options);
    console.log(SUM);

如果没有未来日期,您可以如下查询,

    options[Op.and] = [
        sequelize.where(sequelize.col('created'), {
            [Op.gt]: Sequelize.literal('DATE_SUB(CURDATE(), INTERVAL 1 DAY)')
        })            
    ]
    const SUM = await OrdersModel.sum('price', options);
    console.log(SUM);
于 2021-03-30T18:56:30.163 回答
0

我们还可以使用 [op.between],它将获取两个给定日期范围之间的数据。因此,如果我们给出今天的开始时间和当前时间,它将给出今天的数据。

const Op = Sequelize.Op;
const START = new Date();
START.setHours(0, 0, 0, 0);
const NOW = new Date();

where: {
createdAt: {
    [Op.between]: [START.toISOString(), NOW.toISOString()]
  }
}

快乐的编码...

于 2021-05-21T12:16:14.960 回答