Re: retrieve last n records
| From: | drparker | Date: | Thu, 06 Mar 2003 22:14:37 +0000 |
| Subject: | Re: retrieve last n records | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-138538@lists.php.net to get a copy of this message | ||
I see your point, and I'd really like to use Limit in the query. However,
what if I want to sort those 5 last records by another field? They're already
sorted DESC, so I can't do that.
Kevin Stone wrote:
> ----- Original Message -----
> From: "drparker" <drparker@camden.rutgers.edu>
> To: <php-general@lists.php.net>
> Sent: Thursday, March 06, 2003 2:42 PM
> Subject: [PHP] retrieve last n records
>
> > I need to retrieve the last n records of a table, n will probably be 2
> > or 3. I know that I can accomplish this by: select * from table order
> > by field DESC Limit n, but i would prefer not to do this way because it
> > interferes with my sorting mechanism on the main page. Is there any
> > other way to do it?
>
> Retrieve your records as normal and using mysql_data_seek() to move the
> results pointer to the desired row then start your loop.
> http://www.php.net/manual/en/function.mysql-data-seek.php
>
> Please note this is not an efficient way to accomplish the goal. LIMIT is
> certainly the way to go and you should be able to do any sorting you need
> within the SQL statement. After all retrieving, ordering and grouping
> database information is what SQL was designed to do.
>
> - Kevin