Mysql – Getting last row for each day in MySQL

MySQL

I have problem in selecting other columns for the max(datetime) records. In layman terms I need to get relevant columns where max(DialDateTime) records for all the dates in Mysql.

 mysql> select max(DialDateTime) as max from log_AP group by   date(DialDateTime) ;
 +---------------------+
 | max                 |
 +---------------------+
 | 2012-12-03 07:37:26 | 
 | 2012-12-04 07:37:04 | 
 | 2012-12-05 07:37:04 | 
 | 2012-12-06 07:37:04 | 
 | 2012-12-07 07:37:04 | 
 | 2012-12-08 07:37:04 | 
 | 2012-12-09 07:37:04 | 
 +---------------------+

7 rows in set (0.00 sec)

Best Answer

You should be able to use a subquery to get the max date and then join that to you table to return the remaining columns:

select a1.*
from log_AP a1
inner join
(
  select max(DialDateTime) as max 
  from log_AP 
  group by date(DialDateTime)
) a2
  on a1.DialDateTime = a2.max