从表中选择上一个和下一个记录的最佳方法是允许用户查看给定产品名称的最近产品,例如奶酪
产品表定义为
create table toode (
toode char(20) primary key, -- product code
name char (100) -- product name
)
下面的代码似乎可以工作,但看起来有点难看。在 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;