[英]query: return all rows even if the count of the associated rows are 0
我有這個查詢:
$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
表中沒有結果。 我怎么做?
在主表上設置分組依據
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`
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.