You can use left join
to bring in the rows and coalesce()
to get the 0
instead of NULL
:
select p.name,
(select sum(f.amount)
from forecast f
where v.product_id = p.id),
coalesce(sum(s.amount), 0)
from product p left join
sale s
on sale.product_id = product.id and
sale.outlook > -4
group by p.id
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…