Re: SELECT * FROM foo OFFSET -1 LIMIT 1

From: Markus Bertheau <twanger(at)bluetwanger(dot)de>
To: Martín Marqués <martin(at)bugs(dot)unl(dot)edu(dot)ar>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: SELECT * FROM foo OFFSET -1 LIMIT 1
Date: 2005-06-27 18:41:37
Message-ID: 1119897697.7464.7.camel@localhost.localdomain
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Dnia 27-06-2005, Mo o godzinie 14:17 -0300, Martín Marqués napisał(a):

> How do you skip -1 rows?

You redefine OFFSET to mean "where in the result set to start returning
rows" and give -1 the meaning that I described above.

Please don't ask me, where in the result set position -1 is.

I've stumbled over this because I needed a query which given an id a
result set like this:

id txt pos
23 foo 0
11 bar 1
34 baz 2
13 xop 3

returns the previous and next rows relative to the given id, as ordered
by the position). That would be 23 and 34 for 11, or NULL and 11 for 23.
I'm basically determining the position of the middle row and then
querying with offset pos-1 limit 1 and offset pos+1 limit 1.

Offset for negative numbers means 0, as it seems. I think there is a
sensible meaning for negative offset numbers and wondered, what
arguments led to negative offsets being processed as 0 offset.

Markus

--
Markus Bertheau <twanger(at)bluetwanger(dot)de>

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Greg Stark 2005-06-27 19:35:59 Re: SELECT * FROM foo OFFSET -1 LIMIT 1
Previous Message davide 2005-06-27 18:18:02 Re: How can I simply substatue a value in a query?