You can do this with a clever use of order by
and limit
:
SELECT t.*
FROM test1 t cross join
(SELECT rank FROM test1 t2 WHERE id = @ID) as theone
ORDER BY ABS(theone.rank - t.rank)
LIMIT N;
You probably then want these back in rank order:
SELECT t.*
FROM (SELECT t.*
FROM test1 t cross join
(SELECT rank FROM test1 t2 WHERE id = @ID) as theone
ORDER BY ABS(theone.rank - t.rank)
LIMIT N
) t
ORDER BY t.rank;