SELECT x.val from data x, data y
GROUP BY x.val
HAVING SUM(SIGN(1-SIGN(y.val-x.val))) = (COUNT(*)+1)/2
SET @rowindex := -1;
SELECT
AVG(d.distance) as Median
FROM
(SELECT @rowindex:=@rowindex + 1 AS rowindex,
demo.distance AS distance
FROM demo
ORDER BY demo.distance) AS d
WHERE
d.rowindex IN (FLOOR(@rowindex / 2), CEIL(@rowindex / 2));