如何获取 knex / mysql 中所有更新记录的列表
How to get a list of all updated records in knex / mysql
这是我正在处理的查询:
return knex('table')
.returning('id')
.where('boolean', false)
.andWhere('fooID', foo.id)
.update({
boolean : true
})
.limit(num)
.then(function(ids) {
console.log('\nids');
console.log(ids); //outputs num
ids
现在包含 3,这是受影响的行数。有什么方法可以获取这 3 行的 ID?我的印象是 .returning()
这样做了,但似乎没有。
Mysql 数据库不支持 returning
语句,它 returns 只是更新行的计数 http://dev.mysql.com/doc/refman/5.7/en/update.html。
在您的情况下,您必须首先查询要更新的行的 ID,然后在事务中更新和获取它们。
像这样:
return knex.transaction(trx => {
return trx('table')
.select('id')
.where('boolean', false)
.andWhere('fooID', foo.id)
.limit(num)
.then(ids => {
return trx('table').update({ boolean: true })
.whereIn('id', ids)
.then(() => {
return trx('table').whereIn('id', ids);
});
});
});
这是我正在处理的查询:
return knex('table')
.returning('id')
.where('boolean', false)
.andWhere('fooID', foo.id)
.update({
boolean : true
})
.limit(num)
.then(function(ids) {
console.log('\nids');
console.log(ids); //outputs num
ids
现在包含 3,这是受影响的行数。有什么方法可以获取这 3 行的 ID?我的印象是 .returning()
这样做了,但似乎没有。
Mysql 数据库不支持 returning
语句,它 returns 只是更新行的计数 http://dev.mysql.com/doc/refman/5.7/en/update.html。
在您的情况下,您必须首先查询要更新的行的 ID,然后在事务中更新和获取它们。
像这样:
return knex.transaction(trx => {
return trx('table')
.select('id')
.where('boolean', false)
.andWhere('fooID', foo.id)
.limit(num)
.then(ids => {
return trx('table').update({ boolean: true })
.whereIn('id', ids)
.then(() => {
return trx('table').whereIn('id', ids);
});
});
});