2017-01-12 4 views
1

私は、特定のユーザに対して最も低い集計カラムを持つ行を取得しようとしていて、その1カラムだけインクリメントして返します。これは私が以下に書いたものであり、うまくいくと思っていましたが、そのユーザーのオプションのためにすべての集計列を増やしています。そして再び、私はちょうどあなたがサブクエリでこれを行うことができます最低の集計列...Knoxを使用してポストグルDBの最下位エントリだけをインクリメントします

knex('options').where('user_id', user_id) 
         .orderBy('tally', 'asc') 
         .limit(1) 
         .first() 
         .increment('tally', 1) 
         .then((option) => { 
          console.log(option); 
          res.sendStatus(200) 
}) 
+0

あなたは '.toSQL(とあなたのビルダーから生成されたSQLの種類を確認することができます) '。たとえば、あなたのクエリは次のようなものにレンダリングされます: 'update" options "set" tally "=" tally "+ 1 where" user_id "=?' –

答えて

1

をしたい:

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); 
    }); 
+0

ありがとう!それが私が探していたものです。 –

関連する問題