为什么此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

最佳答案

您可以将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",
      humidity,
      ((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

08-28 03:19