我的查询:
select c.id, sum(case when r.paid_out>='1' then 0 else 1 end) as all_paids
from people p1, houses h, policies p2, receipts r
where p1.id = h.id
and h.code = p2.code
and p2.code_policy = r.code_policy
group by p1.id
having all_paids= 0;
Postesql返回以下错误:
错误:没有列'all_paids'
我已经尝试过几件事,但没有,任何帮助表示赞赏
答案 0 :(得分:4)
首先,学会使用正确的join
语法。
其次,您可以使用having
子句或子查询的表达式:
select p1.id
from people p1 join
houses h
on p1.id = h.id join
policies p2
on h.code = p2.code join
receipts r
on p2.code_policy = r.code_policy
group by p1.id
having sum(case when r.paid_out >= '1' then 0 else 1 end) = 0;
注意:没有定义c.id
,因此我认为select
应该p1.id
基于group by
。
我应该说这种逻辑通常用not exists
表示:
select p1.*
from people p1
where not exists (select 1
from houses h
policies p2
on h.code = p2.code join
receipts r
on p2.code_policy = r.code_policy
where p1.id = h.id and
r.paid_out >= '1'
);
这消除了聚合。