title: MySQL Function Returns Column Value
body: <p>With the help of StackOverflow I have a MySQL query that works. After making a few changes it returns <code>zip</code> and <code>distance</code>. 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. </p>
  
  <pre><code>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 &lt; 5 LIMIT 1;
  </code></pre>
  
  <p>So far this is what I have as my Function:</p>
  
  <pre><code>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 &lt; 5 LIMIT 1;
  
  RETURN zip;
  
  END
  </code></pre>
  
  <p>This does not work, give me and error that there is some wrong in line 10 which is this:</p>
  
  <pre><code>`longitude`)*PI()/180))))*180/PI())*60*1.1515) AS `distance` FROM geov1.zcta loc HAVING distance &lt; 5 LIMIT 1;
  </code></pre>
  
  <p>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.</p>