gpt4 book ai didi

mysql - 根据日期时间和使用名称获取最后一行

转载 作者:行者123 更新时间:2023-11-29 08:00:36 24 4
gpt4 key购买 nike

我在 MYSQL 中有下表:

DATETIME dt
VARCHAR location
FLOAT temperature

该表包含在不同位置测量的许多温度。现在我需要一个 SQL 查询来获取每个位置的最后测量结果。

我实际上不知道该怎么做。

最佳答案

请注意,在执行 group by 聚合时,最好在执行 group by 之前选择列。

select
t1.location,
t2.t2_dt as dt,
t1.temperature
from table_name t1
join
(
select
location,
temperature,
max(dt) as t2_dt
from table_name
group by location
)t2
on t2.location = t1.location AND t1.dt = t2.t2_dt
group by t1.location

<强> DEMO

关于mysql - 根据日期时间和使用名称获取最后一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23927754/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com