我有一个类似于下面表格的表格。 我正在尝试查找今天所有价格的总和。
| 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';