我怎样才能在 Sequelize 中做到这一点?

SELECT ProductID, Name, ListPrice, ListPrice * 1.15 AS NewPrice
FROM Production

我试过了:
db.Production.findAndCountAll(
    attributes: {
        include: [
            ['ListPrice * 1.15', 'NewPrice']
        ]
    }
).then(function(orders){
    return res.jsonp(output);
})

但它不起作用。

这是我期望的查询:
SELECT Production.ProductID, Production.Name, Production.ListPrice, Production.ListPrice * 1.15 AS NewPrice
FROM Production

相反,我看到了这个查询:
SELECT Production.ProductID, Production.Name, Production.ListPrice, ListPrice * 1.15 AS NewPrice
FROM Production

最佳答案

您可以使用 Sequelize.literal 方法。这是代码:

db.Production.findAndCountAll(
    attributes: [
        [Sequelize.literal('ListPrice * 1.15'), 'NewPrice'],
    ]
).then(function(orders){
    return res.jsonp(output);
})

关于javascript - 如何在 Sequelize 中执行算术运算?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39049607/

10-09 21:10