How to get the last record of every day in mysql?

I want to get the last record of every day in mysql. The table Location<id, date, place_id>has several entries every day. This Location table has place_id and the time at which place_id is inserted.

Also, given that place_id is missing, return the second last entry that has place_id. In the following table for the record, NULL, '2016-04-06 18:52:06'we return '13664', '2016-04-06 12:57:30'which is the second last record in '2016-04-06' (March 6) and has place_id.

One more thing, in one day, there will be more place_id, see the following table.

   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 the following query, it returns the following result

Query I run the following query, but it gives me the 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'
+4
3

:

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 FIDDLE DEMO2

[ ]

+2

:

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

, .

:

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

, null :

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

, :

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

Will this work?

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

All Articles