如何根据数据库中的值按某个值及其后继对象来获取对象?
更具体地说,我有表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数据库。
答案 0 :(得分:-1)
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