Re: Counting down

From: John Levine <johnl_at_iecc.com>
Date: Tue, 22 Aug 2017 17:27:32 -0000 (UTC)
Message-ID: <onhpi4$fsq$1_at_gal.iecc.com>


[Quoted] In article <onhksv$ifv$1_at_jstuckle.eternal-september.org>, Jerry Stuckle <jstucklex_at_attglobal.net> wrote:
>On 8/20/2017 11:46 PM, bit-naughty_at_hotmail.com wrote:
>> I have a DB, say 10 rows, with a primary key, numbered 1 to 10. ...

>Forget TNP's "answer". It's his normal crap answer and only works if
>you have exactly 10 rows and the primary keys are 1-10.

Well, yeah.

>What you need is the LIMIT clause, i.e.

[Quoted] [Quoted] It seems to me that we have a somewhat underspecified question. If you know that the records have a primary key and they are all in sequence, which is likely if it's an autoincrement primary key, fetching a record by key is the best approach. If you don't know the number of records, you can start by retrieving that and putting it in a local PHP variable, e.g.:

        SELECT MAX(keycol) FROM mytable

and then

        sprintf "SELECT * FROM mytable where keycol=%d",keycolmax-n

If all you know is that the keys are ascending but you don't know whether they're sequential, then SELECT ... ORDER BY keycol DESC n,1 is reasonable but it's considerably slower since it means mysql has to internally go through the index from the end to find the record rather than doing a direct lookup. Received on Tue Aug 22 2017 - 19:27:32 CEST

Original text of this message