我怎么写Left Join Clause

时间:2017-08-22 10:52:35

标签: mysql laravel laravel-5 left-join

我有如下的MySQL查询

   SELECT AVG(COALESCE(ratings.rating_for_manager, 0)), consultants.id 
   FROM consultants
   LEFT JOIN ratings ON ratings.consultant_id = consultants.id
   GROUP BY  consultants.id

如何在laravel Left Join Clause中编写上面的MySQL连接查询?

3 个答案:

答案 0 :(得分:0)

您可以使用leftJoin

DB::table('consultants')
    ->selectRaw('AVG(COALESCE(ratings.rating_for_manager, 0)), consultants.id ')
    ->leftJoin('ratings', 'ratings.consultant_id', 'consultants.id')
    ->groupBy('consultants.id')
    ->get();

https://laravel.com/docs/5.4/queries#joins

希望这有帮助!

答案 1 :(得分:0)

$psDIR

答案 2 :(得分:0)

你应该试试这个:

$rslt = DB::table('consultants')
    ->leftJoin('ratings','consultants.id','=','ratings.consultant_id')
    ->select('consultants.id',DB::raw('ifnull(AVG(ratings.rating_for_manager),0) as ratingsDetails'))
         ->groupBy('consultants.id')
         ->get();

希望这对你有用!!!