Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

MySQL select to return most recent record

Status
Not open for further replies.

SenTnel

Technical User
Dec 9, 2003
45
DO
Hi!

We have a database that stores vehicle's gps position, date, time, vehicle identification, lat, long, speed, etc., every minute.

The following select pulls each vehicle position and info, but the problem is that returns the first record, and I need the last record (current position), based on date (datagps.Fecha) and time (datagps.Hora). This is the select:

SELECT configgps.Fichagps, datacar.Ficha, groups.Nombre, datagps.Hora, datagps.Fecha, datagps.Velocidad, datagps.Status, datagps.Calleune, datagps.Calletowo, datagps.Temp, datagps.Longitud, datagps.Latitud, datagps.Evento, datagps.Direccion, datagps.Provincia FROM asigvehiculos INNER JOIN datacar ON (asigvehiculos.Iddatacar = datacar.Id) INNER JOIN configgps ON (datacar.Configgps = configgps.Id) INNER JOIN clientdata ON (asigvehiculos.Idgroup = clientdata.group) INNER JOIN groups ON (clientdata.group = groups.Id) INNER JOIN datagps ON (configgps.Fichagps = datagps.Fichagps) Group by Fichagps;

I need same result Im getting, but instead of the older record I need the most recent
(LAST datagps.Fecha / datagps.Hora).


How can I accomplish this?

Thanks a lot!
 
As far as I know, there is no LAST function. But you can always use either the MAX function in combination with a date-time field, or order by the timestamp field and add a LIMIT clause.

I understand too little of the language (Spanish?) to guess if "Hora" is just a time or a date-time field.

If "Fecha" is a date and "Hora" is a time, you should combine them, which will not result in a fast query. But it may be better to store them in a date-time column directly.


+++ Despite being wrong in every important aspect, that is a very good analogy +++
Hex (in Darwin's Watch)
 
Thanks DonQuichote!

I do understand, from what I've been reading, that what I need is the max function, the problem is that Im learning and don't have a clue on how to use it in this case, I mean, im not capable of writing such select.

Your spanish is correct: Hora is just Time, and Fecha is date.

As far as combining I don't know if I should get into that, like I said Im new and learning and don't know why the guy who wrote the app chose this format.

If max() is my shot, how would you write this select?

Thanks again!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top