Postgresql Sequalize postgres-仅当金额大于零时添加金额
我正在寻找一个sequelize查询,这样我就需要一个表的总量。但是有一个条件,只有当它大于0时,才添加数量Postgresql Sequalize postgres-仅当金额大于零时添加金额,postgresql,sequelize.js,Postgresql,Sequelize.js,我正在寻找一个sequelize查询,这样我就需要一个表的总量。但是有一个条件,只有当它大于0时,才添加数量 await model.post.findAll({ where: { type: 'random', }, attributes: [ 'id', 'createdAt', [Sequelize.fn('SUM', Sequelize.col('others.amount')), 'totalAmo
await model.post.findAll({
where: {
type: 'random',
},
attributes: [
'id',
'createdAt',
[Sequelize.fn('SUM', Sequelize.col('others.amount')), 'totalAmount'],
], include: [
{
model: model.other,
attributes: [],
required: false,
duplicating: false,
},
],
order: [['createdAt', 'DESC']],
offset: (args.page - 1) * args.limit,
limit: args.limit,
raw: true,
group: ['posts.id'],
});
在上面的代码中,我只想在金额大于零时添加others.amount
我该怎么做
var list = await db.Post.findAll({
where: {
type: 'random',
},
attributes: [
'id',
'createdAt',
[Sequelize.fn('SUM', Sequelize.col('other.amount')), 'totalAmount'],
],
include: [
{
model: db.Other,
attributes: [],
required: false,
duplicating: false,
as: 'other',
where: {
amount: { [Op.gte]: 0 } // grater than 0
}
},
],
order: [['createdAt', 'DESC']],
raw: true,
group: ['Post.id'],
});
console.log(JSON.parse(JSON.stringify(list)))
添加了其中:…
内部包括