我想在日期之后计算一堆条目。但是,我也希望得到结果为0.但是当我按日期子句添加过滤器时,当前查询会丢弃所有0结果。任何想法如何获得这0个条目?
select distinct (e.customer_id), count(m.id) as msgs from eng_msgs e
join messages m on e.customer_id = m.customer_id
where e.customer_id = m.customer_id
and m.created_at > e.created_at -- this line removes 0 results
group by e.customer_id
答案 0 :(得分:1)
您需要outer join
条件:
select e.customer_id, count(m.id) as msgs
from
eng_msgs e
left outer join
messages m on
e.customer_id = m.customer_id
and
m.created_at > e.created_at
group by e.customer_id