Laravel 5.6 DB :: RAW count()

时间:2018-12-15 06:24:39

标签: laravel

如何计算score列中得分小于50的行数?

 $user = DB::table('users')
 ->leftJoin('rankings', 'rankings.user_id', '=', 'users.id')
 ->select('users.*', DB::raw("(  (count(rankings.score)  ) )  as `countBelow50` ")

1 个答案:

答案 0 :(得分:0)

检查高级连接子句:https://laravel.com/docs/5.7/queries#joins

$user = DB::table('users')
      ->leftJoin('rankings', function($join) {
                $join->on('rankings.user_id', '=', 'users.id')
                           ->where('rankings.score','<','50');
             })
      ->select('users.*', DB::raw("(  (count(rankings.score)  ) )  as `countBelow50` ")