id timing temp temp1 temp2 temp3
5260 1446746934 -76 -15 4 25
5259 1446746332 -75 -14 5 25
5258 1446745731 -77 -15 6 25
5257 1446734343 -71 -10 0 21
我有上面的数百个数据。数据库将获取新值并插入到新行5261.我尝试在temp列中选择前三行平均值,例如,此值为-76,-75,-77。所以平均值应该返回-76。我在SQL代码中知道错误。
SELECT avg(temp) as temperature from tempArray order by id DESC limit 3;
答案 0 :(得分:1)
SELECT avg(t.temp) as temperature
from (select temp from tempArray order by id DESC limit 3) t;
(通过id DESC limit 3从tempArray顺序中选择temp)t ---选择表中的前三个条目,并将此表命名为't'。然后计算平均值
答案 1 :(得分:0)
将Group by
与AVG
SELECT avg(temp) as temperature from tempArray where id
in( SELECT * from (SELECT id temperature from tempArray order by id DESC limit 3) as t)