在同一查询中使用字段别名?

时间:2014-09-16 06:24:22

标签: sql oracle oracle8i

如何...或可以我在同一个select-clause和where-clause中使用字段别名a.k.a. field as val

select
    a.LONG_NAME_I_CANT_REMEMBER as val1,
    a.NAME_I_DONT_WANT_2_REMEMBER as val2,
    val1-val2 as diff
from
    random_table a
where
    (val1 > 12)

没有内联视图/内联查询,而不是关于表别名

1 个答案:

答案 0 :(得分:3)

您无法在SELECT子句的WHERE子句中使用您定义的别名。逻辑上,在确定要显示哪些列(WHERE列表)之前确定要返回哪些行(应用SELECT子句),以便在处理WHERE子句时,别名为&#39 ; t存在。

当然,您可以重构查询,以便在内联视图中选择和别名数据,然后在该视图外部应用WHERE子句。像

这样的东西
select a.*,
       val1-val2 as diff
  from (select a.LONG_NAME_I_CANT_REMEMBER as val1,
               a.NAME_I_DONT_WANT_2_REMEMBER as val2,
          from random_table a) a
 where (val1 > 12)