mysql获取具有限制的几个列的平均值

时间:2013-04-10 23:36:48

标签: mysql

我试图从3列中获得最后60个项目的平均值,但是我得到了一个错误:

#1248 - Every derived table must have its own alias

这是查询:

SELECT AVG( Sensor1 ) , AVG( Sensor2 ) , AVG( Sensor3 ) 
FROM (

    SELECT Sensor1, Sensor2, Sensor3
    FROM temperatur
    ORDER BY Zeit DESC 
    LIMIT 0 , 60
)

我相信Alias相信......

2 个答案:

答案 0 :(得分:4)

MySQL需要在所有派生表和子查询上使用别名,并且在结束括号后缺少别名:

SELECT AVG( d.Sensor1 ) , AVG( d.Sensor2 ) , AVG( d.Sensor3 ) 
FROM 
(
    SELECT Sensor1, Sensor2, Sensor3
    FROM temperatur
    ORDER BY Zeit DESC 
    LIMIT 0 , 60
) d --- add this

答案 1 :(得分:2)

我猜的是语法,但我相信你需要更像这样的东西:

SELECT AVG(T.Sensor1),AVG(T.Sensor2),AVG(T.Sensor3) FROM(

SELECT Sensor1, Sensor2, Sensor3
FROM temperatur
ORDER BY Zeit DESC 
LIMIT 0 , 60

)T