gpt4 book ai didi

mysql - mysql如何获取每24小时的最后一条记录?

转载 作者:行者123 更新时间:2023-11-30 22:21:50 25 4
gpt4 key购买 nike

在下表中,我想返回过去 24 小时跨度内的每条最后记录。还要考虑如果 place_id 不存在则返回具有 place_id 的倒数第二条记录。在下表中,对于 NULL, '2016-04-06 18:52:06' 记录,我们返回 '13664', '2016-04-06 12:57:30',这是 24 小时内的倒数第二条记录,具有 place_id。

   id  ||  place_id || date
'1', '47', '2016-04-05 18:09:37'
'2', '48', '2016-04-05 12:09:37'
'3', '13664', '2016-04-06 12:57:30'
'4', '9553', '2016-04-08 10:09:37'
'5', NULL, '2016-04-06 18:52:06'
'6', '9537', '2016-04-07 03:34:24'
'7', '9537', '2016-04-07 03:34:24'
'8', '656', '2016-04-07 05:34:24'
'9', '7', '2016-04-07 05:34:57'

当我运行以下查询时,它返回以下结果

查询 我更改了查询,但它给我 NULL

select S1.place_id, S1.date from 
status TTS1
Left join
status TTS2
on
Date(S1.date) = Date(S2.date)
And
S1.date < S2.date
where
S2.date is null and S1.location_id=38298

group by S1.date;

结果

'47', '2016-04-05 18:09:37'
NULL, '2016-04-06 18:52:06'
'7', '2016-04-07 05:34:57'
'9553', '2016-04-08 10:09:37'

但是这个结果是不正确的,是给NULL,如果有NULL则返回当天的倒数第二条..

正确结果:

'47', '2016-04-05 18:09:37'    
'13664', '2016-04-06 12:57:30'
'9553', '2016-04-08 10:09:37'
'7', '2016-04-07 05:34:57'

最佳答案

试试这个

select distinct  s.place_id, s.date from base_status s 
where s.locatoin_id=38298 group by Date(s.date) order by s.date DESC;

关于mysql - mysql如何获取每24小时的最后一条记录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36467952/

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