Använd:
SELECT x.a,
x.b,
x.distance
FROM (SELECT t.a,
t.b,
t.distance
CASE
WHEN @distance != t.distance THEN @rownum := 1
ELSE @rownum := @rownum + 1
END AS rank,
@distance := t.distance
FROM TABLE t
JOIN (SELECT @rownum := 0, @distance := '') r
ORDER BY t.distance --important for resetting the rownum variable) x
WHERE x.rank <= 2
ORDER BY x.distance, x.a