我有以下查询给出了结果,但未按预期进行
select g.id
, g.name
, COUNT(p1.id) as total
from ft_smsender_groups as g
, ft_smsender_persons as p1
,ft_smsender_persons2groups as p2
where g.id = p2.group_id
and p1.id = p2.person_id
group
by id
, name
这为我提供了与每个人相关联的群组的价值,
但它正在跳过那些没有任何人关联的群体,
我需要修改此查询以向我提供所有组名,以便我没有分配任何人,它应该将total
列值赋予0
答案 0 :(得分:-1)
试试这个
select g.id,g.name, count(p2.id) as total from ft_smsender_
groups as g left join (select p1.id,p2.group_id
from ft_smsender_persons2groups as p2 inner join ft_smsender_persons
as p1 on p1.id = p2.person_id ) as p2 on g.id = p2.group_id group by id,name