这工作正常。如果我更改 @unit_type 变量。正确的单位类型出现在计算中。
set @unit_type = 'kilometers';
set @lat = 50;
set @lon = 100;
set @lat2 = 51;
set @lon2 = 101;
select
(CASE @unit_type
WHEN 'feet' THEN 20000000
WHEN 'miles' THEN 3696.1
WHEN 'meters' THEN 6979100
WHEN 'kilometers' THEN 6979
END
* acos( cos( radians(@lat) )
* cos( radians( @lat2 ) )
* cos( radians( @lon2 )
- radians(@lon) )
+ sin( radians(@lat) )
* sin( radians( @lat2 ) )
) ) AS distance;
然而,事实并非如此。当我尝试运行这个存储过程时,它每次都会以公里为单位返回。如果我删除公里的情况,结果为空。
CREATE PROCEDURE `anotherTest`(IN `lat` BIGINT, IN `lon` BIGINT, IN `lat2` BIGINT, IN `lon2` BIGINT, IN `unit_type` TEXT)
LANGUAGE SQL
NOT DETERMINISTIC
CONTAINS SQL
SQL SECURITY DEFINER
COMMENT ''
BEGIN
select
(CASE @unit_type
WHEN 'feet' THEN 20000000
WHEN 'miles' THEN 3696.1
WHEN 'meters' THEN 6979100
WHEN 'kilometers' THEN 6979
END
* acos( cos( radians(@lat) )
* cos( radians( @lat2 ) )
* cos( radians( @lon2 )
- radians(@lon) )
+ sin( radians(@lat) )
* sin( radians( @lat2 ) )
) ) AS distance;
END
我只是想了解为什么会这样,以及如何更好地编写来解决这个问题。
有人有想法么?提前致谢。
更新:
解决方案是从我的过程的 SELECT 语句中删除 @'s。因为它们不需要访问参数。
CREATE PROCEDURE `anotherTest`(IN `lat` BIGINT, IN `lon` BIGINT, IN `lat2` BIGINT, IN `lon2` BIGINT, IN `unit_type` TEXT)
LANGUAGE SQL
NOT DETERMINISTIC
CONTAINS SQL
SQL SECURITY DEFINER
COMMENT ''
BEGIN
select
(CASE unit_type
WHEN 'feet' THEN 20000000
WHEN 'miles' THEN 3696.1
WHEN 'meters' THEN 6979100
WHEN 'kilometers' THEN 6979
END
* acos( cos( radians(lat) )
* cos( radians( lat2 ) )
* cos( radians( lon2 )
- radians(lon) )
+ sin( radians(lat) )
* sin( radians( lat2 ) )
) ) AS distance;
END