到目前为止,我的查询看起来像这样......
{
where: {
id: event.pathParameters.picId
},
'include': [{
'model': db.reputations,
'where': {
'type': 'V_UP'
},
'as': 'up_reputations',
'required': false
}, {
'model': db.reputations,
'where': {
'type': 'V_DONW'
},
'as': 'down_reputations',
'required': false
}],
'attributes': [
'id',
'title',
'data',
'createdAt',
[db.Sequelize.fn('count', db.Sequelize.col('up_reputations.type')), 'upVotes'],
[db.Sequelize.fn('count', db.Sequelize.col('down_reputations.type')), 'downVotes']
]
}
我需要的是最后一个属性,score
,即 upVotes
- downVotes
但我不知道该怎么做。
最佳答案
这应该有效:
'attributes': [
'id',
'title',
'data',
'createdAt',
[db.Sequelize.fn('count', db.Sequelize.col('up_reputations.type')), 'upVotes'],
[db.Sequelize.fn('count', db.Sequelize.col('down_reputations.type')), 'downVotes'],
[db.Sequelize.literal('(upVotes - downVotes)'), 'score']
]
关于javascript - 在 Sequelize 中减去 2 个字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43443245/