Re: Getting 'n-1'th record.

Поиск
Список
Период
Сортировка
Искать

Re: Getting 'n-1'th record.

От:
"Jeff Eckermann" <jeckermann@verio.net>
Дата:

Re: Getting 'n-1'th record.

От:
Tom Lane <tgl@sss.pgh.pa.us>
Дата:

Re: Getting 'n-1'th record.

От:
"omid omoomi" <oomoomi@hotmail.com>
Дата:

Getting 'n-1'th record.

От:
Bhuvan A <bhuvansql@yahoo.com>
Дата:

Re: Getting 'n-1'th record.

От:
"Josh Berkus" <josh@agliodbs.com>
Дата:
Bhuvan,

> An sql query results with 'n' records. OK.
> I need ONLY the 'n-1'th record.

You're making this much harder than it needs to be.

If you want the "nth" record, then you have to be supplying the database
with an ORDER BY. For the next-to-last record, simply reverse the ORDER
BY and take the second record.

e.g.:

If you want the next-to-last (n-1) record from:
SELECT * FROM syslog 
ORDER BY entrytime;

Then ask for:
SELECT * FROM syslog 
ORDER BY entrytime DESC 
LIMIT 1 OFFSET 1;

Easy, no?

-Josh Berkus

______AGLIO DATABASE SOLUTIONS___________________________
                                       Josh Berkus
  Complete information technology      josh@agliodbs.com
   and data management solutions       (415) 565-7293
  for law firms, small businesses        fax 621-2533
    and non-profit organizations.      San Francisco
FAQ