最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to Perform Arithmetic Operations in Sequelize? - Stack Overflow

programmeradmin5浏览0评论

How can I do this in sequelize?

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

I've tried:

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

But it doesn't work.

This is the query that I expect:

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

Instead, I see this query:

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

How can I do this in sequelize?

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

I've tried:

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

But it doesn't work.

This is the query that I expect:

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

Instead, I see this query:

SELECT Production.ProductID, Production.Name, Production.ListPrice, ListPrice * 1.15 AS NewPrice  
FROM Production
Share Improve this question asked Aug 20, 2016 at 1:22 VicheanakVicheanak 6,70420 gold badges67 silver badges99 bronze badges 2
  • Any progress on this? – Airton Gessner Commented Oct 14, 2016 at 13:41
  • yeah bro. already hand that project to my client. I end up, wrote a raw query for that one. I'm wondering if I do this might work: ['Production.ListPrice * 1.15', 'NewPrice']. You can try as now i'm busy on another project. – Vicheanak Commented Oct 14, 2016 at 13:45
Add a ment  | 

1 Answer 1

Reset to default 7

You can use Sequelize.literal method. Here is the code:

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

评论列表(0)

  1. 暂无评论