Reputation: 23
I know that the DistanceInMiles function works, I tested it. But why doesn't the query return any results. I'm new to sql so I don't really know how to do complex queries, but I don't know why this isn't working. All of the Items in the database meet the requirements of the query. But it returns 0 row(s) returned.
DELIMITER $$
DROP FUNCTION IF EXISTS DistanceInMiles$$
CREATE FUNCTION DistanceInMiles(src_lat decimal(10, 8), src_lng decimal(11, 8), dest_lat decimal(10, 8), dest_lng decimal(11, 8))
RETURNS decimal(15, 8) DETERMINISTIC
BEGIN
RETURN CAST((3959 * acos(cos(radians(src_lat)) * cos(radians(dest_lat)) *
cos(radians(dest_lng) - radians(src_lng)) + sin(radians(src_lat)) *
sin(radians(dest_lat)))) as decimal(15, 8));
END $$
DELIMITER ;
SET @dist = 2500;
SET @srctLat = 41.024449;
SET @srcLng = -72.481628;
SELECT z.lat, z.lng, z.name, z.restaurant_id,
DistanceInMiles(@srcLat, @srcLng, z.lat, z.lng) as distance
from restaurants z
HAVING distance < @dist
ORDER BY distance
LIMIT 10
Upvotes: 0
Views: 33
Reputation: 868
You made a typo.
Change SET @srctLat = 41.024449;
to SET @srcLat = 41.024449;
Upvotes: 1