2017-03-06 43 views
0

我想用knex.js得到最大計數明顯

bookshelf.knex('user').distinct('social_name') 
     .select('social_image_url') 
     .count('status as score').innerJoin('game', function(){ 
      this.on('game.user_id', 'user.id'); 
     }).innerJoin('round', function(){ 
      this.on('round.game_id', 'game.id'); 
     }).where('status', 'win') 
     .groupBy('game.id', 'user.id') 
     .orderBy('score', 'desc') 
     .limit(10) 
     .then(function(top10){ 
      callback(null, top10); 
     }).catch(function(err){ 
      callback(err); 
     }); 

擺脫數據庫最大計數,但是這是我得到

[ { social_name: 'B', social_image_url: '.....', score: 4 }, 
    { social_name: 'A', social_image_url: '.....', score: 3 }, 
    { social_name: 'A', social_image_url: '.....', score: 2 } ] 

我的social_name: A兩條記錄,而不是得到它只有最大它是

{ social_name: 'A', social_image_url: '.....', score: 3 } 

回答

0

我終於解決了這個問題,通過創建子查詢fi rst然後使用它的內部連接。