我有以下代码片段:
const buys = await Buy.findAll({
where: {
account_id: req.params.id,
},
attributes: ['id', 'account_id', 'buy_value', 'quantity'],
});
return res.json(buys);
它返回以下 JSON:{
"id": 9,
"account_id": 3,
"buy_value": "49521.00",
"quantity": 2
}
我想在同一个 JSON 中返回一个 test_field
乘以 buy_value
和 quantity
字段。我该怎么做 ?
最佳答案
只需像这样使用 Sequelize.literal :
const buys = await Buy.findAll({
where: {
account_id: req.params.id,
},
attributes: ['id', 'account_id', 'buy_value', 'quantity', [Sequelize.literal('buy_value*quantity'), 'test_field']],
});
关于node.js - 将 Sequelize 中的字段相乘选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63022690/