gpt4 book ai didi

postgresql - 从 Postgresql View 获取下一条记录

转载 作者:行者123 更新时间:2023-11-29 13:29:57 29 4
gpt4 key购买 nike

我已经根据我的要求创建了一个 View someView

ID  |   name
3 | A
4 | F
8 | G
13 | E
26 | Z

当我在 where 条件下传递 ID 时,我需要从 View 中获取下一条记录IE。如果我通过 4 我应该回到 8 。另外,如果我传递最后一个 ID,它应该返回第一条记录IE。如果我超过 26,我应该回到 3。

我一直在尝试使用滞后和超前,但没有任何效果。我也试过这个

DECLARE aaa SCROLL CURSOR FOR SELECT ID FROM someView where ID > someValue;
fetch 1 from aaa

在这种情况下,我确实获得了下一个值,但如果我传递了最后一个 ID,它不会获得结果。那我该怎么办呢??还有其他方法可以解决我的问题吗?

提前致谢。

最佳答案

像这样:

with next_entry as (
select *
from sometable
where id > 26 -- change the desired value here
order by id
limit 1
)
select *
from next_entry
union all
select *
from sometable
where not exists (select 1 from next_entry)
limit 1;

它可能不是很有效,但应该可以。

关于postgresql - 从 Postgresql View 获取下一条记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26193375/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com