如何从数据库中获取对象及其后续对象?

ssm49v7z  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(257)

如何根据某个值从数据库中获取对象及其后继对象?
更具体地说,我有一张table articles 带列 id , name , order . 我想用特定的 order 以及它的继任者 order 价值观。我的查询应该是什么样的?
例子
数据:

id name order
1   A   14
2   B   27
3   C   18
4   D   86
5   E   39
6   F    2

提问:

FetchTwoByOrderASC(18) => (C,B) // 18 is C and the successor of 18 in this case is 28 which is B --> {(F,2)-(A,14)-(C,18)-(B,27)-(E,39)-(D,86)}
FetchTwoByOrderASC(39) => (E,D) // 39 is E and the successor of 39 in this case is 86 which is D --> {(F,2)-(A,14)-(C,18)-(B,27)-(E,39)-(D,86)}
FetchTwoByOrderASC(14) => (A,C) // 14 is A and the successor of 14 in this case is 18 which is C --> {(F,2)-(A,14)-(C,18)-(B,27)-(E,39)-(D,86)}

我使用mysql数据库。

lfapxunr

lfapxunr1#

Select name from table where order>=given_order order by order limit 2 #mysql

或者

Select top (2) name from table where order>=given_order order by order #sqlserver

相关问题