得到最大数量明显

时间:2017-03-06 09:47:33

标签: knex.js bookshelf.js

我希望使用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 }

1 个答案:

答案 0 :(得分:0)

我最后通过首先创建子查询来解决这个问题,然后再使用它进行内连接。

相关问题