如何在MySQL中的另一列计算中重用动态列?

时间:2010-10-12 14:08:22

标签: sql mysql

我有这样的疑问:

SELECT 
  IF(daycode=1,(SELECT...),(SELECT...)) AS weekavg,
  (SELLOFF1 / weekavg) AS procent
FROM .....

它告诉我:Unknown column 'weekavg' in 'field list',这是在我添加了除法之后发生的,之后就可以了。

1 个答案:

答案 0 :(得分:1)

您可以使用子查询:

SELECT (selloff1 / weekavg) AS procent
FROM (
  SELECT 
    selloff1, IF(daycode=1,(SELECT...),(SELECT...)) AS weekavg
  FROM ...
)