【问题标题】:How to get a list of all updated records in knex / mysql如何获取 knex / mysql 中所有更新记录的列表
【发布时间】:2017-05-26 16:56:23
【问题描述】:

这是我正在处理的查询:

  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 node.js knex.js


    【解决方案1】:

    Mysql 数据库不支持returning 语句,它只返回更新行数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);
            });
        });
    });
    

    【讨论】:

    • 很好的答案。我在任何地方都找不到此信息。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-08
    • 1970-01-01
    • 1970-01-01
    • 2016-07-02
    • 1970-01-01
    • 2011-08-04
    • 1970-01-01
    相关资源
    最近更新 更多