我有一个包含三列和几百万行数据的表。
sensor_data
sensor_id int PK
date datetime PK,
data decimal (10,3)
此查询返回传感器4的最新日期,但传感器4的最旧数据:
select max(date), data
from sensor_data
where sensor_id = 4;
这些查询返回正确的数据值:
select date, data
from sensor_data
where sensor_id = 4
and date = (select max(date) from sensor_data where sensor_id = 4)
order by date desc;
select date, data
from sensor_data
where sensor_id = 4
order by date desc
limit 1;
我是MySQL的新手。我使用max()
错了,或者这是它应该如何工作的?