简体   繁体   中英

query: return all rows even if the count of the associated rows are 0

i have this query:

$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');

which translates to the following query:

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

However, if there are no results in the trienios table, the query will only return a single record from the oe table. I want to return all the results from the oe table even if there are no results in the trienios table. How do I do that?

在主表上设置分组依据

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`

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM