用别名计算

时间:2016-11-05 12:28:46

标签: mysql sql

简单问题:
我可以使用别名来计算吗?是

SELECT example1 / example2 AS ratio
     , SQRT(ratio) AS squareroot
FROM example_table

合法且有特殊规则(在使用之前定义别名等)吗?

1 个答案:

答案 0 :(得分:1)

不,您应该在使用前定义别名,所以要么:

select example1 / example2 as ratio
     , sqrt(example1 / example2) as squareroot 
from example_table

select ratio
     , sqrt(ratio) as squareroot
from ( 
  select example1 / example2 as ratio 
  from example_table
) as tbl