I'm trying to select stores from a database within 3 miles of a location. I have the longitude and latitude in the database stored in radians to help ease runtime computation.
I want to get distance in the select, where, and order by clauses. In order not to have to do the trig multiple timesI want to use an alias. The order by works fine, but the I can't get the select to work.
The code with just the order by that works looks like
Adding in the where clause blows-up
I'm really weak at sql, so any help appreciated.
I want to get distance in the select, where, and order by clauses. In order not to have to do the trig multiple timesI want to use an alias. The order by works fine, but the I can't get the select to work.
The code with just the order by that works looks like
Code:
SELECT name, ( ACOS( SIN( 0.6598463936 ) * SIN( lat_radians ) + COS( 0.6598463936 ) * COS( lat_radians ) * COS( lon_radians - - 2.1343445941 ) ) * 3956 ) AS dist
FROM stores
ORDER BY `dist` ASC
Adding in the where clause blows-up
Code:
SELECT name, ( ACOS( SIN( 0.6598463936 ) * SIN( lat_radians ) + COS( 0.6598463936 ) * COS( lat_radians ) * COS( lon_radians - - 2.1343445941 ) ) * 3956 ) AS dist
FROM stores
WHERE `dist` < 3
ORDER BY `dist` ASC
I'm really weak at sql, so any help appreciated.