我想在结果3字段中检索每个用户的walls
,follow
和exos
计数。
以下代码为每个字段提供相同的计数:
$profils = \DB::table('users')
->select(['users.*',\DB::raw('count(follow.user_id) as likeme'), \DB::raw('count(murs.user_id) as murme'), \DB::raw('count(exos.user_id) as exome')])
->leftjoin('murs', 'users.id', '=', 'murs.user_id')
->leftjoin('exos', 'users.id', '=', 'exos.user_id')
->leftjoin('follow', 'users.id', '=', 'follow.user_id')
->groupby('users.id')
->paginate(15);
答案 0 :(得分:0)
尝试以下代码:
$profils = \DB::table('users')
->select(['users.*',\DB::raw('count(DISTINCT(follow.user_id)) as likeme'), \DB::raw('count(DISTINCT(murs.user_id)) as murme'), \DB::raw('count(DISTINCT(exos.user_id)) as exome')])
->leftjoin('murs', 'users.id', '=', 'murs.user_id')
->leftjoin('exos', 'users.id', '=', 'exos.user_id')
->leftjoin('follow', 'users.id', '=', 'follow.user_id')
->groupby('users.id')
->paginate(15);
如果这不适合你,请告诉我。