I can take this place, but I find this an interesting problem. I want this to be a more open discussion, as I think I could learn a lot.
It seems that this can be achieved by executing several selection statements - something like SELECT id FROM mytable WHERE source="A" ORDER BY distance ASC LIMIT 1 . After completing something like this in the while loop and replacing "A" with the id variable, it will do the trick, no?
For example (A - source, B - final destination):
DECLARE var_id as INT WHILE var_id != 'B' BEGIN SELECT id INTO var_id FROM mytable WHERE source="A" ORDER BY distance ASC LIMIT 1 SELECT var_id END
Is it something like this job? (The code is sloppy, but the idea seems sound.) Comments are more than welcome.
source share