Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
How to retrieve last or first record from MySQL table ?
Message
 
 
À
13/02/2009 03:35:52
Information générale
Forum:
Visual FoxPro
Catégorie:
Visual FoxPro et .NET
Versions des environnements
Visual FoxPro:
VFP 9 SP1
OS:
Windows XP SP2
Network:
Windows XP
Database:
MySQL
Divers
Thread ID:
01381516
Message ID:
01381583
Vues:
47
>Hii, I am really new in using remote data and I really need some help.
>
>How to retrieve last or first record from MySQL table ?
>How to navigate in MySQL table just like using skip command ? eq. skip 1, skip -1, go top, go bottom
>
>Thanks

You cannot Go Top, Go Bottom, or Skip, but you can do something that gives you the same result.

If you have an autoincrement field, you can use the value of that field to locate the last or first record, or to skip from record to record.

I do not use autoincrement fields because I do not need to "Go Top" or "Skip", but I have a field that I have in all MySQL tables that store the "CreateTime" of the record. I can show you how the code would look like using my CreateTime field, but as I said before for your purpose you should use an autoincrement field.
When saving a new record I store the DATETIME() value in this field. So to get the last or first record of the MySQL table you could select on this field:
SQLEXEC(1,"SELECT MAX(CreateTime) AS CreateTime FROM MyTable","TempAlias")
ptCreateTime = TempAlias.CreateTime
SQLEXEC(1,"SELECT * FROM MyTable WHERE CreateTime = ?ptCreateTime","ResultAlias")
In order to get the "first" record you use MIN(CreateTime) instead of MAX(CreateTime).

It is not foolproof in a multi-user environment, because there can be multiple inserts happening in the same time, but the chance is that you get only one or two matches. So that means in this case you really need to use an autoincrement field for this purpose.

If you want to "skip" in chronological order you can use a similar approach:
*-- Skip to the next record.
ptCreateTime = ResultAlias.CreateTime
SQLEXEC(1,"SELECT * FROM MyTable WHERE CreateTime > ?ptCreateTime ORDER BY CreateTime LIMIT 0,1","ResultAlias")
Hope that helps.
Christian Isberner
Software Consultant
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform