Query Before After Record (MySQL)

set @id=2;
SELECT id, nama FROM (
  SELECT m.*, @n1:=@n1 + 1 num, @n2:=IF(id = @id, @n1, @n2) pos
    FROM request m, (SELECT @n1:=0, @n2:=0) n ORDER BY m.id
) t
WHERE @n2 >= num - 1 AND @n2 <= num + 1

atau

SELECT * FROM (
  SELECT 
    m.*, 
    @n1:=@n1 + 1 num, 
    @n2:=IF(id={$id}, @n1, @n2) pos
  FROM gallery m, (SELECT @n1:=0, @n2:=0) n ORDER BY m.id ) t
WHERE @n2 >= num - 1 AND @n2 <= num + 1

Published by

G3n1k

just to remember what i had known :)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s