这让我完全难住了。从一组数字 - 对于这个例子 1 到 20,我试图做上一个和下一个链接,所以它们最终是这样的:我将提供开始的数字。
1 6 11 16
2 7 12 17
3 8 13 18
4 9 14 19
5 10 15 20
n-5 p-6 n-10 p-11 n-15 p-16
你知道如何为这样的事情做 sql 吗?
我试图建立在这个基础上,但没有运气:
SELECT c.id,
(SELECT MAX(p.id) FROM mytable p WHERE p.id < c.id AND p.country = 'us') prev_id,
(SELECT MIN(n.id) FROM mytable n WHERE n.id > c.id AND n.country = 'us') next_id
FROM mytable as c WHERE c.id = 5;
表数据
"id" "country"
"1" "US"
"2" "US"
"3" "US"
"4" "US"
"5" "US"
"6" "US"
"7" "US"
"8" "US"
"9" "US"
"10" "US"
"11" "US"
"12" "US"
期望的输出
我提供的数字 = 10
prev | next
-------------
6 | 10