0

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

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

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

4

1 回答 1

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;
于 2013-10-12T18:58:05.500 回答