临时栏“不存在?”

时间:2013-10-12 18:55:26

标签: postgresql select

我有一个名为adjust_status的存储函数和一个列users的表status

select
  adjust_status(status) as adjusted_status
from users
  where adjusted_status > 0;

它说adjusted_status不存在。为什么?我该如何解决这个问题?

1 个答案:

答案 0 :(得分:4)

您无法在定义的同一级别上引用别名。您需要使用派生表:

select *
from (
  select adjust_status(status) as adjusted_status
  from users
) t
where adjusted_status > 0;

当然可以延长:

select *
from (
  select adjust_status(u.status) as adjusted_status, 
         u.some_other_column, 
         f.yet_another_column
  from users u
     join foo f on f.some_id = u.some_other_id
) t
where adjusted_status > 0;