gpt4 book ai didi

mysql查询提供数据但跳过空值

转载 作者:行者123 更新时间:2023-11-30 23:47:15 25 4
gpt4 key购买 nike

我有以下查询,它给出了结果但不是预期的结果

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

这给了我与每个人相关联的群体的值(value),

但它会跳过那些没有任何人关联的组,

我需要修改此查询以提供所有组名,因此我没有分配任何人,它应该将 total 列值指定为 0

最佳答案

这样试试

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

关于mysql查询提供数据但跳过空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31758404/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com