简体   繁体   English

如何在mysql中获取每天的最后一条记录?

[英]How to get last record of each day in mysql?

I want to get last record of each day in mysql.我想在 mysql 中获取每天的最后一条记录。 Location<id, date, place_id> table has multiple entries on each day. Location<id, date, place_id>表每天都有多个条目。 This Location table has place_id and time at which place_id is inserted.此 Location 表具有 place_id 和插入 place_id 的时间。

Also taking consider if place_id is not present then return second last record which has place_id.还要考虑如果 place_id 不存在则返回具有 place_id 的倒数第二条记录。 In following table for NULL, '2016-04-06 18:52:06' record we are returning '13664', '2016-04-06 12:57:30' , which is second last record on '2016-04-06' (6th March) and has place_id.在下表中,对于NULL, '2016-04-06 18:52:06'记录我们返回'13664', '2016-04-06 12:57:30' ,这是 '2016-04- 的倒数第二条记录06'(3 月 6 日)并具有 place_id。

One more thing, on single day, there would be more place_id, see the following table..还有一点,在一天之内,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'

When I run following query it returns following result当我运行以下查询时,它返回以下结果

Query I run the following query but it is giving me wrong result查询我运行以下查询,但它给了我错误的结果

`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'

You may give it a try:你可以试一试:

SELECT 
L.id,
L.place_id,
L.date
FROM Location L
INNER JOIN 
(
  SELECT 
   MAX(date) max_time
  FROM Location
  GROUP BY Date(`date`)
) AS t
ON L.date = t.max_time

SQL FIDDLE DEMO SQL 小提琴演示

SQL FIDDLE DEMO2 SQL 小提琴演示 2

[ Based on your expected output ] [基于您的预期输出]

Can you try with the following query:您可以尝试使用以下查询吗:

SELECT * FROM `Location` GROUP BY DATE(`date`) ORDER BY `date` DESC

What this query does is group the rows by descending date and show a row for each date.此查询的作用是按日期降序对行进行分组,并为每个日期显示一行。

Get the last record:获取最后一条记录:

SELECT * FROM `Location` ORDER BY `date` LIMIT 1;

Get the last record that doesn't have a null as a value:获取没有null作为值的最后一条记录:

SELECT * FROM `Location` WHERE place_id IS NOT NULL ORDER BY `date` LIMIT 1;

Get records for all the places which are not null:获取所有不为空的地方的记录:

SELECT * FROM `Location` WHERE place_id IS NOT NULL GROUP BY `place_id` ORDER BY `date` DESC

Would this work?这行得通吗?

select place_id, max(date) as MaxDate
from foo
where place_id is not NULL
group by place_id

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM