The problem is that you cannot reference columns with an alias ( distance in this case) in a select or where clause. For example, you cannot do this:
select a, b, a + b as NewCol, NewCol + 1 as AnotherCol from table where NewCol = 2
This will happen in both cases: the select statement when trying to process NewCol + 1 , and also in the where statement when trying to process NewCol = 2 .
There are two ways to solve this problem:
1) Replace the reference to the calculated value itself. Example:
select a, b, a + b as NewCol, a + b + 1 as AnotherCol from table where a + b = 2
2) Use an external select statement:
select a, b, NewCol, NewCol + 1 as AnotherCol from ( select a, b, a + b as NewCol from table ) as S where NewCol = 2
Now, taking into account your HUGE and not very convenient calculation column for people :) I think that you need to go to the last option to increase readability:
SET @orig_lat=55.4058; SET @orig_lon=13.7907; SET @dist=10; SELECT * FROM ( SELECT *, 3956 * 2 * ASIN(SQRT(POWER(SIN((@orig_lat -abs(latitude)) * pi()/180 / 2), 2) + COS(@orig_lat * pi()/180 ) * COS(abs(latitude) * pi()/180) * POWER(SIN((@orig_lon - longitude) * pi()/180 / 2), 2) )) as distance FROM geo_kulplex.sweden_bobo ) AS S WHERE distance < @dist ORDER BY distance limit 10;
Edit: As indicated below, this will result in a full table scan. Depending on the amount of data that you will process, you can avoid this and move on to the first option, which should run faster.