我在MySQL数据库(level_records)中有一个表,该表具有3列(id,日期,阅读)。我想将最近20个读数(按日期)之间的差异放入数组中,然后对它们进行平均,以找到平均差异。
我到处都看过,但似乎没人能像我一样。
我将非常感谢您的帮助。谢谢。
SELECT AVG(difference) FROM ( SELECT @next_reading - reading AS difference, @next_reading := reading FROM (SELECT reading FROM level_records ORDER BY date DESC LIMIT 20) AS recent20 CROSS JOIN (SELECT @next_reading := NULL) AS var ) AS recent_diffs
演示