如何为连接表包含空值?

时间:2016-06-23 00:26:46

标签: php sql laravel

即使该代码没有相关结果,我如何修改我的查询以包含职业代码?

应显示代码(我有6个职业代码),如果没有该代码的条目,则计数应显示为0.

$maori = $request->exists('maori');
$years = collect([2011,2012,2013,2014,2015,2016]);

$providers = $years->map(function($year) use ($maori){
    $query = DB::table('vocations')
    ->selectRaw('vocations.*,count(*) as count')
    ->leftjoin('standard_vocation', 'vocations.id','=','standard_vocation.vocation_id')
    ->leftjoin('standards','standard_vocation.standard_id','=','standards.id')
    ->leftjoin('results','standards.id','=','results.id')
    ->leftjoin('learner_provider','results.learner_provider_id','=','learner_provider.id');


    if($maori) {
        $query->join('learners','learner_provider.learner_id','=','learners.id')
        ->join('learner_ethnicity','learners.id','=','learner_ethnicity.learner_id')
        ->leftjoin('ethnicities','ethnicities.id','=','learner_ethnicity.ethnicity_id')
        ->where('ethnicities.code','211');
    }

    $query->where('learner_provider.year',$year);
    $query->groupBy('vocations.id');


    return $query->get();
});

请帮忙。

感谢。

All items should have 6 children, empty ones should have count 0

0 个答案:

没有答案