我有这个问题:
$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
表中没有结果也是如此。我该怎么做?
答案 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`