List:General Discussion« Previous MessageNext Message »
From:Micah Stevens Date:November 12 2008 1:43pm
Subject:Re: Row before and after?
View as plain text  
Select the UserId one less, and then ORDER ASC LIMIT 3.

Assuming your UserId's are sequential, it's easy, given userID X

SELECT * FROM Users WHERE UserId = X-1 ORDER BY UserId ASC LIMIT 3;

If they're not sequential due to deletions, etc, it becomes a bigger
problem. You could do a subquery, but that would only be marginally
faster than two queries.

Sorry if I'm not more creative in the morning. :)

-Micah

On 11/12/2008 01:10 AM, Waynn Lue wrote:
> Whoops, just realized I made a mistake in the examples.  What I'm really
> looking for is these two queries:
>
> SELECT * FROM Users WHERE UserId > *userid*;
> SELECT * FROM Users WHERE UserId < *userid*;
>
> Waynn
>
> On Wed, Nov 12, 2008 at 12:14 AM, Waynn Lue <waynnlue@stripped> wrote:
>
>   
>> I'm trying to find the first row before and the first row after a specific
>> row.  Essentially I want to do these two queries, and get each row.
>>
>> SELECT * FROM Users WHERE UserId = <userId> ORDER BY UserId DESC LIMIT 1;
>> SELECT * FROM Users WHERE UserId = <userId> ORDER BY UserId LIMIT 1;
>>
>> Is there any way to combine this into one query?  OFFSET doesn't allow a
>> negative number, which is essentially what I want.
>>
>> Thanks,
>> Waynn
>>
>>     
>
>   

Thread
Row before and after?Waynn Lue12 Nov
  • Re: Row before and after?Waynn Lue12 Nov
    • Re: Row before and after?Micah Stevens12 Nov
      • Re: Row before and after?Peter Brawley12 Nov
  • RE: Row before and after?Jerry Schwartz12 Nov
  • Re: Row before and after?Dan Nelson12 Nov
    • Re: Row before and after?Waynn Lue16 Nov