Mysql 如何显示数据库的最新值?
我有这样的标签: 我要显示名称和温度的最新值,怎么做 我尝试了SQL查询:Mysql 如何显示数据库的最新值?,mysql,sql,Mysql,Sql,我有这样的标签: 我要显示名称和温度的最新值,怎么做 我尝试了SQL查询: SELECT name, temperature,MAX(Time) AS max_time FROM marker WHERE Time = max_time ; 但它并没有达到预期的效果 预期结果: Sensor #1 8 2016-06-05 01:00:00 Sensor #1 8 2016-06-05 01:00:00 Sensor #1 8 2016-06-05 01:00:00 Sens
SELECT name, temperature,MAX(Time) AS max_time
FROM marker
WHERE Time = max_time
;
但它并没有达到预期的效果
预期结果:
Sensor #1 8 2016-06-05 01:00:00
Sensor #1 8 2016-06-05 01:00:00
Sensor #1 8 2016-06-05 01:00:00
Sensor #1 8 2016-06-05 01:00:00
Sensor #1 8 2016-06-05 01:00:00
我是这样决定的:
SELECT name, temperature, Time
FROM marker
WHERE Time = (SELECT MAX(Time) FROM marker);
这应该行得通。在您的查询中,由于子查询中没有
where
子句,因此它会尝试匹配所有行的最大时间,我认为这不是预期的结果。期望的结果是什么?我添加了期望的结果。你可以检查一下。现在请解释一下背后的逻辑。@easy\u peasy。期望的结果毫无意义。为什么同一行要重复五次?我有个错误:“max_time是未知列”。
SELECT name, temperature, time
FROM marker m
WHERE timeval= (SELECT MAX(time) FROM marker p where p.name=m.name);