Knex.js - How To Update a Field With An Expression(Knex.js - 如何使用表达式更新字段)
问题描述
我们如何让 Knex 创建以下 SQL 语句:
How do we get Knex to create the following SQL statement:
UPDATE item SET qtyonhand = qtyonhand + 1 WHERE rowid = 8
我们目前正在使用以下代码:
We're currently using the following code:
knex('item')
.transacting(trx)
.update({qtyonhand: 10})
.where('rowid', 8)
但是,为了让我们的库存应用程序在多用户环境中工作,我们需要 qtyonhand 值与当时数据库中的实际值相加或相减,而不是传递一个在更新语句被执行.
However, in order for our inventory application to work in a multi-user environment we need the qtyonhand value to add or subtract with what's actually in the database at that moment rather than passing a value that may be stale by the time the update statement is executed.
推荐答案
这里有两种不同的方法
knex('item').increment('qtyonhand').where('rowid',8)
或
knex('item').update({
qtyonhand: knex.raw('?? + 1', ['qtyonhand'])
}).where('rowid',8)
这篇关于Knex.js - 如何使用表达式更新字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Knex.js - 如何使用表达式更新字段
基础教程推荐
- 从字符串 TSQL 中获取数字 2021-01-01
- MySQL根据从其他列分组的值,对两列之间的值进行求和 2022-01-01
- 带有WHERE子句的LAG()函数 2022-01-01
- 如何在 CakePHP 3 中实现 INSERT ON DUPLICATE KEY UPDATE aka upsert? 2021-01-01
- MySQL 5.7参照时间戳生成日期列 2022-01-01
- 带更新的 sqlite CTE 2022-01-01
- while 在触发器内循环以遍历 sql 中表的所有列 2022-01-01
- ORA-01830:日期格式图片在转换整个输入字符串之前结束/选择日期查询的总和 2021-01-01
- CHECKSUM 和 CHECKSUM_AGG:算法是什么? 2021-01-01
- 使用 VBS 和注册表来确定安装了哪个版本和 32 位 2021-01-01
