更新时间:2023-12-03 18:42:22
一种可能的解决方案是
SQL语句
SELECT ABS(ID - 9), *
FROM MyTable
ORDER BY
ABS(ID - 9)
LIMIT 5
编辑 (向ypercube表示此解决方案可能存在的缺陷)
如果要从左侧获取2个id,从右侧获取2个id,则可以按以下方式调整该语句
If the intent is to get 2 id's from the left and two id's from the right, the statement can be adjusted as follows
SELECT * FROM MyTable WHERE ID <= 9 ORDER BY ID DESC LIMIT 3
UNION ALL
SELECT * FROM MyTable WHERE ID > 9 ORDER BY ID ASC LIMIT 2