A readings table stores sensor_id, reading_time and value. I want the most recent reading of every sensor. SELECT sensor_id, MAX(reading_time), value FROM readings GROUP BY sensor_id fails with an error saying value must appear in the GROUP BY clause or be used in an aggregate function. On an old MySQL server the same query runs but the values are sometimes from the wrong row.
Why is the query rejected, and what is the correct way to get the whole latest row per sensor?




