Mysql
 sql >> Base de Dados >  >> RDS >> Mysql

Consulta MySQL, MAX() + GROUP BY


(Testado no PostgreSQL 9.algo)

Identifique a eliminação e o carimbo de data/hora.
select rid, max(timestamp) as ts
from test
group by rid;

1   2011-04-14 18:46:00
2   2011-04-14 14:59:00

Junte-se a ele.
select test.pid, test.cost, test.timestamp, test.rid
from test
inner join 
    (select rid, max(timestamp) as ts
    from test
    group by rid) maxt
on (test.rid = maxt.rid and test.timestamp = maxt.ts)