我想获得mysql中每天的最后一条记录。
还要考虑如果没有place_id,则返回具有place_id的第二个最后记录。 在下表中,对于
还有一件事,在单个日期上,会有更多的place_id,请看下面的表格。
当我运行以下查询时,它返回以下结果:
查询:我运行了以下查询,但是它给出了错误的结果。
Location<id,date,place_id>
表在每天都有多个条目。该Location表具有place_id和插入place_id的时间。还要考虑如果没有place_id,则返回具有place_id的第二个最后记录。 在下表中,对于
NULL,'2016-04-06 18:52:06'
记录,我们返回'13664','2016-04-06 12:57:30'
,它是“2016-04-06”(3月6日)上的倒数第二个记录,并且具有place_id。还有一件事,在单个日期上,会有更多的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'
当我运行以下查询时,它返回以下结果:
查询:我运行了以下查询,但是它给出了错误的结果。
`Location<id, place_id, date>`
select L1.place_id, L1.date from
Location1 L1
Left join
Location1 L2
on
Date(L1.date) = Date(L2.date)
And
L1.date < L2.date
where
L2.date is null
group by L1.date;
Result I want:
id....place_id ........date
'1', '47', '2016-04-05 18:09:37'
'3', '13664', '2016-04-06 12:57:30'
'4', '9553', '2016-04-08 10:09:37'
'9', '7', '2016-04-07 05:34:57'
place_id
相对应的id
。如果你需要与place_id
相对应的id
,那么你可以使用SQL FIDDLE DEMO2
。或者,如果你需要与max_date
字段相对应的id
,那么你可以使用SQL FIDDLE DEMO
。 - 1000111id
字段与您的要求,以及 DEMO 2 中的id
字段与您的要求。 - 1000111