how to return the last date in timestamp doing the group by of the same id in mysql

0

I have a problem I have a table that has the serial column, equipment and a timestamp date has no primary key because this table would be a history, I in the table I have several lines with the same serial but I need to bring the last date only I need to bring in a single line, but when I put the group by serial order by timestamp desc it does not bring the last inserted date as that I would do this follows the query below:

SELECT * FROM spare_change.cad_checklist group by serial order by timestamp desc ;

The return would be

serial | equipamento |         data 
  60   |    item     | 2010-04-05 10:30:58

The last date would be for example the following 2010-04-06 09:59:10

    
asked by anonymous 19.04.2018 / 13:40

2 answers

0

You can use MAX () to get the largest date and group by the other:

SQLFiddle - Online Example:

SELECT 
  serial
  , equipamento
  , max(data) AS DataMaisRecente
FROM 
  cad_checklist 
GROUP BY 
  serial
  , equipamento
ORDER BY 
  DataMaisRecente DESC;
    
19.04.2018 / 14:00
0

This will probably work for you.

SELECT serial, max(data )
  FROM spare_change.cad_checklist 
 GROUP BY serial;

So I would not need order by, so I would only return a single line

    
19.04.2018 / 14:00