javascript - 在 Sequelize 中减去 2 个字段

标签 javascript sequelize.js

到目前为止,我的查询看起来像这样......

{
    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/

相关文章:

javascript - 是否可以使用 SignalR 更新 Google map 中的标记?

node.js - 将 Sequelize 与 RDS 一起使用获取 TimeoutError : ResourceRequest timed out

node.js - Sequelize - 在验证模型关联时获得正确的路径

javascript - 在 Javascript 中的 Get Cookie 中获取破折号后的值

javascript - Nivoslider(在动态 ajax 内容中)不会在第一次加载时加载图像

sequelize.js - 无法删除 Sequelize 查询中的模型属性

mysql - 如何在与sequ​​elize Node 的关系上使用having子句

node.js - 仅从 Sequelize ORM 中获取 dataValues

javascript - 如果 X 类不存在,则 Jquery/Javascript 添加类

javascript - 为页面加载时不存在的内容设置动画