I have the following table:
CREATE TABLE post (
id bigint primary key,
thread_id bigint,
is_notice boolean,
title text,
content text
)
I display the list using the following query:
SELECT * FROM post ORDER BY is_notice desc, thread_id desc, id
Then, given the post selected by id(i.e. SELECT * FROM post where id=3
), how do I retrieve the next and previous posts?
Best Answer
Using PostgreSQL's Window Functions, specifically
LAG
andLEAD
, should be able to show you the previous and next entries in your table.A demo can be found here: http://sqlfiddle.com/#!15/9fd7a/8