简体   繁体   中英

MySQL location select query returns 0 in stored procedure

Can anybody spot the problem here? I have a stored procedure I wanna use to find closest locations to a specific location.

When I try the select outside of a stored proc (just in a query window) all is fine.

SELECT  id, 
        ( 3959*acos( 
            cos(radians(37)) *
            cos(radians(lat)) *
            cos(radians(lng) - radians(-122)) + 
            sin(radians(37)) * 
            sin(radians(lat)) 
            ) 
        ) AS distance 
FROM markers 
HAVING distance < 25 
ORDER BY distance 
LIMIT 0 , 20;

But when I place it in a stored proc it always return 0.

DELIMITER $$

CREATE PROCEDURE `GetLocationsByRadius`(IN latitude double, IN longitude double,IN  radius double)
begin

  SELECT  ( 6371 * acos( cos( radians(latitude) ) * cos( radians( Latitude ) ) * cos( radians( Longitude ) - radians(longitude ) ) + sin( radians(latitude) ) * sin( radians( Latitude ) ) ) ) AS distance FROM LocationTrades HAVING distance < radius ORDER BY distance LIMIT 0 , 20;


end $$

I tried changing the table data types from float4 to float8 to decimals, I tried using inner variables inside the stored, but nothing helps, it always returns distance 0. Seems like it thinks something is INT inside there...

Any help would be appreciated.

Thanks

Ok, I found the problem.

The table columns need table identifiers before them. (Table.Column)

This works:

DELIMITER $$

CREATE PROCEDURE GetLocationsByRadius (IN latitude double, IN longitude double,IN radius double) begin

SELECT ( 6371 * acos( cos( radians(latitude) ) * cos( radians( Locations.Latitude ) ) * cos( radians( Locations.Longitude ) - radians(longitude) ) + sin( radians(latitude) ) * sin( radians( Locations.Latitude ) ) ) ) AS distance FROM Locations HAVING distance < radius ORDER BY distance LIMIT 0 , 20;

end $$

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM