Laravel:使用mergeBindings()的复杂查询

时间:2018-07-05 19:40:20

标签: php laravel laravel-query-builder

如果对项目进行排序,我将获得行号? 此查询工作正常

SELECT @rank:=@rank+1 AS rank,pts from(
    SELECT DISTINCT             
    users.id,
    username,
    SUM(points) AS pts,
    @rank:=@rank+1 AS rank,
    count(users_games.user_id) AS CountGame,
    (SELECT count(user_id) FROM users_games WHERE points=3 AND user_id=users.id ) AS win,
    (SELECT count(user_id) FROM users_games WHERE points=0 AND user_id=users.id ) AS los,
    (SELECT count(user_id) FROM users_games WHERE points=1 AND user_id=users.id ) AS draw
    FROM users_games,users 
    WHERE users_games.user_id=users.id
    AND score_home IS NOT NULL
    AND score_away IS NOT NULL 
    AND points IS NOT NULL 
    GROUP BY users.id
    ORDER BY pts DESC,win DESC,draw DESC,CountGame DESC
    ) t1, (SELECT @rank:=0) t2

但是我想像这样在查询生成器中使用它:

 $result= DB::table('users_games')
         ->join('users','users.id','=','users_games.user_id')
         ->select('users.id',
                  'username',
                   DB::raw('SUM(points) as pts'),
                   DB::raw('count(users_games.user_id) as CountGame'),
                   DB::raw('(select count(user_id) from users_games where points=3 and user_id=users.id) as win'),
                   DB::raw('(select count(user_id) from users_games where points=0 and user_id=users.id) as los'),
                   DB::raw('(select count(user_id) from users_games where points=1 and user_id=users.id) as draw'))
         ->whereNotNull('users_games.score_home')
         ->whereNotNull('users_games.score_away')
         ->whereNotNull('users_games.points')
         ->orderBy('pts','desc')
         ->orderBy('win','desc')
         ->orderBy('draw','desc')
         ->orderBy('CountGame','desc')
         ->groupBy('users.id');
         ->get();

我用谷歌搜索并找到了该函数mergeBindings(),我的查询现在看起来像这样,但是不起作用:

 $sub= DB::table('users_games')
         ->join('users','users.id','=','users_games.user_id')
         ->select('users.id',
                  'username',
                   DB::raw('SUM(points) as pts'),
                   DB::raw('count(users_games.user_id) as CountGame'),
                   DB::raw('(select count(user_id) from users_games where points=3 and user_id=users.id) as win'),
                   DB::raw('(select count(user_id) from users_games where points=0 and user_id=users.id) as los'),
                   DB::raw('(select count(user_id) from users_games where points=1 and user_id=users.id) as draw'))
         ->whereNotNull('users_games.score_home')
         ->whereNotNull('users_games.score_away')
         ->whereNotNull('users_games.points')
         ->orderBy('pts','desc')
         ->orderBy('win','desc')
         ->orderBy('draw','desc')
         ->orderBy('CountGame','desc')
         ->groupBy('users.id');

$result=DB::table( DB::raw("({$sub->toSql()}) as sub") )
          ->select(DB::raw('@rank:=@rank+1 AS rank'),'pts')
          ->mergeBindings($sub->getQuery())
          ->get();

我该如何解决这个问题?

0 个答案:

没有答案