Re: Counting down

From: <bit-naughty_at_hotmail.com>
Date: Sun, 27 Aug 2017 23:56:19 -0700 (PDT)
Message-ID: <a4c5e0cd-86c1-4ee3-ae2c-e9860a44ea5e_at_googlegroups.com>


[Quoted] On Tuesday, August 22, 2017 at 9:42:22 PM UTC+5:30, Jerry Stuckle wrote:
> > I have a DB, say 10 rows, with a primary key, numbered 1 to 10. Now, according to a PHP variable I have, say $i, I would like to fetch the 10th - $i field in the DB, ie. if $i is 3, then I wanna retrieve the 7th field in the DB. (it'll have to be sorted in DESC order, right?)
> > How do I word the SQL statement, putting the $i into it? The quoting and stuff confuses me.... :)
> >
> >
> > Thanks.
> >
>
> 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.
>
> What you need is the LIMIT clause, i.e.
>
> SELECT col1, col2, col3 FROM mytable ORDER BY keycol DESC LIMIT 3,1;
>
> Play with the first LIMIT value to get the exact row you want.

[Quoted] Yes, and what would the *PHP* line look like? - I get confused with the quoting, as I said.

$query=sprintf("select myfields from mytable order by keycol desc limit $i,1");

  • is that it?
Received on Mon Aug 28 2017 - 08:56:19 CEST

Original text of this message