How to get the latest record from the database
Stefan Apelt <[email protected]> Mon, 21 Jun 2004 18:14:18 +0200
| Newsgroups | gmane.comp.web.webobjects.devel,gmane.comp.web.webobjects.eof |
|---|---|
| Message-ID | <[email protected]> |
Hi, sorry for the crosspost, I am not sure how many people are reading the eof-list. My problem is as follows: I have a long list of records for user actions, simplified like this: time user action 18:12 user foo opened document 18:12 user bar pasted paragraph 18:13 user foo opened another document The problem is that there are a lot of these entries in the database. Now I want to specify a user and get only the latest entry for that user (the one with the highest timestamp). The latest timestamp could also be some time in the past so I cannot compare with the current time. My solution 'til now would be to fetch all records for the user, sort them by time and return the first (or last) element in the resulting array. However, since there can be literally thousands of records, this is going to be slow. Does anyone have a better idea to produce SQL for only fetching the one record in question? I would be very grateful for that! -- Cheers, Stefan