使用 Knex 仅增加 postgres 数据库中的最低条目
Only increment the lowest entry in postgres DB using Knex
我正在尝试为特定用户获取具有最低计数列的行,然后仅递增该列,然后 return 它。这就是我在下面写的,我认为它会起作用,但它会增加该用户选项的每个计数列。再一次,我只想要计数最低的列 ...
knex('options').where('user_id', user_id)
.orderBy('tally', 'asc')
.limit(1)
.first()
.increment('tally', 1)
.then((option) => {
console.log(option);
res.sendStatus(200)
})
你可以用子查询来做到这一点:
knex('options')
.increment('tally', 1)
.whereIn('id', subQuery => {
subQuery('options')
.select('id')
.where('user_id', user_id)
.orderBy('tally', 'asc')
.limit(1);
})
.then(option => {
console.log(option);
res.sendStatus(200);
});
我正在尝试为特定用户获取具有最低计数列的行,然后仅递增该列,然后 return 它。这就是我在下面写的,我认为它会起作用,但它会增加该用户选项的每个计数列。再一次,我只想要计数最低的列 ...
knex('options').where('user_id', user_id)
.orderBy('tally', 'asc')
.limit(1)
.first()
.increment('tally', 1)
.then((option) => {
console.log(option);
res.sendStatus(200)
})
你可以用子查询来做到这一点:
knex('options')
.increment('tally', 1)
.whereIn('id', subQuery => {
subQuery('options')
.select('id')
.where('user_id', user_id)
.orderBy('tally', 'asc')
.limit(1);
})
.then(option => {
console.log(option);
res.sendStatus(200);
});