到目前为止,我的查询看起来像这样......
{
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
,但我不知道如何做到这一点。
答案 0 :(得分:4)
这应该有效:
'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']
]
答案 1 :(得分:0)
尝试
[db.Sequelize.literal('COUNT(`up_reputations.type`) - COUNT(`down_reputations.type`)'), 'score']