PostgreSQL:使用别名列时的情况
我想这样做:PostgreSQL:使用别名列时的情况,postgresql,Postgresql,我想这样做: select case when (select count(*) as score from users t1 ) >5 THEN score else 0 end 当我尝试时,我得到错误: column score doesn't exists. 我可以用其他的方法吗?我需要它来设定一个极限值。我当然想这样做: select case when (select count(*) as score from users t1 ) >5
select
case when (select count(*) as score from users t1 ) >5 THEN score else 0 end
当我尝试时,我得到错误:
column score doesn't exists.
我可以用其他的方法吗?我需要它来设定一个极限值。我当然想这样做:
select
case when (select count(*) as score from users t1 ) >5 THEN (select count(*) as score from users) else 0 end
但是我需要执行两次相同的查询。
有人有什么想法吗 您可以将
与子句一起使用:
with a as (select count(*) score from t)
select case when score > 5 then score else 0 end from a;
或子查询(内联视图):
通用表表达式呢?它可以帮助您。文档链接:
select case when score > 5 then score else 0 end
from (select count(*) score from t) t;