With the help of StackOverflow I have a MySQL query that works. After making a few changes it returns zip and distance. I been trying to make a Function to set parameters for the given lat and lng values and return the zip from the other table. However the query returns two values, I only need one.
SELECT loc.zip,(((ACOS(SIN((lat*PI()/180)) * SIN((`latitude`*PI()/180))+COS((lat*PI()/180)) * COS((`latitude`*pi()/180)) * COS(((lng - `longitude`)*PI()/180))))*180/PI())*60*1.1515) AS `distance` FROM geov1.zcta loc HAVING distance < 5 LIMIT 1;
So far this is what I have as my Function:
BEGIN
DECLARE lat VARCHAR(15);
DECLARE lng VARCHAR(15);
DECLARE zip BIGINT;
SET zip = SELECT loc.zip,(((ACOS(SIN((lat*PI()/180)) * SIN((`latitude`*PI()/180))+COS((lat*PI()/180)) * COS((`latitude`*pi()/180)) * COS(((lng - `longitude`)*PI()/180))))*180/PI())*60*1.1515) AS `distance` FROM geov1.zcta loc HAVING distance < 5 LIMIT 1;
RETURN zip;
END
This does not work, give me and error that there is some wrong in line 10 which is this:
`longitude`)*PI()/180))))*180/PI())*60*1.1515) AS `distance` FROM geov1.zcta loc HAVING distance < 5 LIMIT 1;
At this point I am at a loss, I just need to have the query return the zip with the given coordinates. Thank you for the help.
If your SELECT statement returns two values, then you should use SELECT statement with INTO clause, e.g. –