Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL knex math operation

I'm using knex with node.js I want to do a math query (I want to add +1 to already existing values)

I want to do this in knex

UPDATE fares SET fare=fare + 70 

This is my query in knex, all I get is 0 in the database

knex('table').update({ fares: 'fares' + 70}).then(function() {});
like image 966
Morris S Avatar asked Feb 05 '26 19:02

Morris S


1 Answers

You don't need to use .raw(), although it will work fine. There is built-in capability to increment using .increment(). See: doc link

Sample from the docs:

knex('accounts')
  .where('userid', '=', 1)
  .increment('balance', 10)

... and, using your table/field names, and a value variable for the update amount.

knex('fares')
  .increment('fare', fareIncrementAmount)

Happy coding!

like image 79
GaryL Avatar answered Feb 08 '26 14:02

GaryL



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!