0

简单的问题,但我似乎无法找到一个简单的解决方案:

给定以下数据,根据当前步骤编号获取上一步和下一步唯一 HEX ID 的最有效方法是什么:

----+-------------+----------------
ID | 非qID | 步数 |
----+-------------+----------------
21 | BcDeF | 1 |
22 | 吉隆坡 | 2 |
23 | MnOPq | 3 |
24 | RsTuV | 4 |
25 | wXyZa | 5 |
----+-------------+----------------

例如:
假设提供的步骤是3,我怎样才能获得两者的唯一 HEX ID24

谢谢!


编辑
这是我目前正在使用的,但对我来说似乎没有那么高效?

SELECT
    nxt.stepnum AS NextStep,
    nxt.unqID AS NextUnqID,
    prv.stepnum AS PrevStep,
    prv.unqID AS PrevUnqID
FROM foo w,
(
 SELECT stepnum, unqID FROM foo WHERE stepnum > 3 ORDER BY stepnum LIMIT 1
) AS nxt
,
(
 SELECT stepnum, unqID FROM foo WHERE stepnum < 3 AND ORDER BY stepnum DESC LIMIT 1
) AS prv
WHERE w.stepnum = 3
4

1 回答 1

2

我修改了你的查询。虽然它有点嵌套,但它应该由非常简单的 sql 引擎索引查找组成(理论上......)

SELECT
    nxt.stepnum AS NextStep,
    nxt.unqID   AS NextUnqID,
    prv.stepnum AS PrevStep,
    prv.unqID   AS PrevUnqID

FROM (SELECT NULL) AS dummy -- dummy table to LEFT JOIN to

LEFT JOIN
( SELECT stepnum, unqID FROM foo
  WHERE stepnum = (SELECT MIN(stepnum)
                   FROM foo
                   WHERE stepnum > 5 )
) AS nxt ON true

LEFT JOIN
( SELECT stepnum, unqID FROM foo
  WHERE stepnum = (SELECT MAX(stepnum)
                   FROM foo
                   WHERE stepnum < 5 )
) AS prv ON true
于 2012-06-22T18:36:18.393 回答