为什么SQL查询将错误未知列赋予别名

为什么此sql查询给出错误?

SELECT
  dt_created AS "time",humidity,((temp* 1.8) + 32) AS temp_f,(-42.379 + 2.04901523 * temp_f + 10.14333127*humidity - .22475541* temp_f * humidity - .00683783 * temp_f* temp_f - .05481717*humidity*humidity + .00122874 * temp_f * temp_f*humidity + .00085282 * temp_f*humidity*humidity - .00000199 * temp_f * temp_f*humidity*humidity)  AS  "Feels Like"
FROM particle_photon
WHERE
  dt_created BETWEEN FROM_UNIXTIME(1572739408) AND FROM_UNIXTIME(1572825808)
ORDER BY dt_created;

错误为Error 1054: Unknown column 'temp_f' in 'field list' 但是我确实以别名声明了temp_f

lindatech 回答:为什么SQL查询将错误未知列赋予别名

您可以将alias用作columns的一部分。您需要将其放入subquery中并进行必要的计算。

select time,humidity,temp_f,(-42.379 + 2.04901523 * temp_f + 10.14333127*humidity - .22475541* temp_f * humidity - .00683783 * temp_f* temp_f - .05481717*humidity*humidity + .00122874 * temp_f * temp_f*humidity + .00085282 * temp_f*humidity*humidity - .00000199 * temp_f * temp_f*humidity*humidity)  AS  "Feels Like"
from (select
      dt_created as "time",((temp* 1.8) + 32) AS temp_f
    from particle_photon
    where
      dt_created between FROM_UNIXTIME(1572739408) AND FROM_UNIXTIME(1572825808)
    order by dt_created) as t1

如果不想使用temp_f,请用((temp* 1.8) + 32)替换subquery

本文链接:https://www.f2er.com/3169672.html

大家都在问