query:即使关联行的计数为0,也返回所有行

时间:2017-07-05 12:08:30

标签: php mysql laravel

我有这个问题:

$oe = OE::select([
    'oe.id',
    'oe.id_cartao', 
    'oe.nome', 
    'oe.email',
    \DB::raw('count(trienios.oe_id) as count')
])->leftjoin('trienios', 'trienios.oe_id', '=', 'oe.id')
->groupBy('trienios.oe_id');

转换为以下查询:

select `oe`.`id`, `oe`.`id_cartao`, `oe`.`nome`, `oe`.`email`, count(trienios.oe_id) as count 

from `oe` 

left join `trienios` on `trienios`.`oe_id` = `oe`.`id` 

group by `trienios`.`oe_id

但是,如果trienios表中没有结果,则查询将仅返回oe表中的单个记录。我想返回oe表中的所有结果,即使trienios表中没有结果也是如此。我该怎么做?

1 个答案:

答案 0 :(得分:0)

  

在主要表格上设置分组

select `oe`.`id`, `oe`.`id_cartao`, `oe`.`nome`, `oe`.`email`, count(trienios.oe_id) as count from `oe` left join `trienios` on `trienios`.`oe_id` = `oe`.`id` group by `oe`.`id`
相关问题