Which is best way to select previous and next records from table to allow user to see also nearest products for given product name, e.q Cheese
product table is defined as
create table toode (
toode char(20) primary key, -- product code
name char (100) -- product name
)
Code below seems to work but looks a bit ugly. Which is best way to implement this in Postgres?
CREATE temp table toodevalik1 on commit drop as
SELECT *
FROM toode
WHERE name >= 'Cheese'
order by name, toode
limit 50;
CREATE temp table toodevalik2 on commit drop as
SELECT *
FROM toode
WHERE name < 'Cheese'
order by name desc, toode desc
limit 50;
SELECT *
FROM toodevalik1
union all
SELECT *
FROM toodevalik2
order by name, toode;