Laravel: сложный запрос с использованием mergeBindings () - PullRequest
0 голосов
/ 05 июля 2018

Я получу номер строки, если элементы отсортированы? Этот запрос отлично работает

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

но я хочу использовать его с Query Builder следующим образом:

 $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();

как я могу решить эту проблему?

...