2017-08-03 44 views
0

我想計算一些日期過後的條目。但是,我也想要結果說0。但是,當我添加按日期條款過濾器時,當前查詢將全部0結果丟棄。任何想法如何獲得這些0條目?即使在0時選擇計數postgres

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 

回答

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