Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Perform Arithmetic Operations in Sequelize?

Tags:

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
like image 411
Vicheanak Avatar asked Aug 20 '16 01:08

Vicheanak


1 Answers

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);
})
like image 160
Arunas Stonis Avatar answered Sep 22 '22 16:09

Arunas Stonis