MySQL 用户定义的纬度经度语法函数
我创建了一个 MySQL 函数来确定一组纬度和经度坐标是否在另一组纬度和经度坐标的特定范围内。但是,该函数给了我一个语法错误,因此我无法测试它是否正常工作。任何有助于找出导致错误的原因的帮助将不胜感激。该函数及其描述如下:
它的工作原理是将起始纬度/经度坐标传递给函数。该数据库包含行 targa、targb 和 targc,它们分别包含要比较的纬度、经度和范围。数据库中的 targ 列指定是否应检查该行的纬度/经度范围。
CREATE FUNCTION inrange(
lat1 decimal(11, 7),
lon1 decimal(11, 7))
READS SQL DATA
RETURNS INT(1)
BEGIN
DECLARE distance decimal(18, 10);
SET distance = ACOS(SIN(lat1)*SIN(targ2)+COS(lat1)*COS(targ2)*COS(targ3-lon1))*6371;
IF distance <= targ4 THEN
RETURN 1;
END IF;
RETURN 0;
END$$
mysql 给我的错误是:
1064 - 你的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,了解在“READS SQL DATA”附近使用的正确语法 返回整数(1) 开始 在第 4 行声明距离小数(18, 10)'
我似乎不知道如何克服这个错误。
另外,如果有人来到这篇文章寻找这样的 mysql 函数,我还有另一篇相关文章:
我非常感谢另一位发帖人提供的函数(这也是这个函数的灵感),但我需要更紧凑的东西。因此,有问题的功能。
编辑:以下代码可以正常运行。只需记住将分隔符设置为 $$ 即可。再次感谢大家的帮助。
工作代码:
CREATE FUNCTION inrange(
lat1 decimal(11, 7),
long1 decimal(11, 7),
lat2 decimal(11, 7),
long2 decimal(11, 7),
rng decimal(18, 10))
RETURNS INT(1)
BEGIN
DECLARE distance decimal(18, 10);
SET lat1 = lat1 * PI() / 180.0,
long1 = long1 * PI() / 180.0,
lat2 = lat2 * PI() / 180.0,
long2 = long2 * PI() / 180.0;
SET distance = ACOS(SIN(lat1)*SIN(lat2)+COS(lat1)*COS(lat2)*COS(long2-long1))*6371;
IF distance <= rng THEN
RETURN 1;
END IF;
RETURN 0;
END$$
I have created a MySQL function to determine if a set of latitude and longitude coordinates are within a certain range of another set of latitude and longitude coordinates. However, the function is giving me a syntax error so I cannot test to see if it is working properly. Any help figuring out what is causing the error would be greatly appreciated. The function along with a description of it is written below:
It works by having the starting lat/long coordinates passed to the function. The database contains the rows targa, targb, and targc, that contain the latitude, longitude, and range (respectively) to compare to. The targ column in the database specifies whether or not this row should be checked against for latitude/longitude range.
CREATE FUNCTION inrange(
lat1 decimal(11, 7),
lon1 decimal(11, 7))
READS SQL DATA
RETURNS INT(1)
BEGIN
DECLARE distance decimal(18, 10);
SET distance = ACOS(SIN(lat1)*SIN(targ2)+COS(lat1)*COS(targ2)*COS(targ3-lon1))*6371;
IF distance <= targ4 THEN
RETURN 1;
END IF;
RETURN 0;
END$
The error that mysql is giving me is:
1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'READS SQL DATA
RETURNS INT(1)
BEGIN
DECLARE distance decimal(18, 10)' at line 4
I can't seem to figure out how to get past this error.
Also, if anyone came to this post looking for such a mysql function, I also had another related post:
MySQL Function to Determine Zip Code Proximity / Range
I'm very grateful to the other poster for his function (which served as the inspiration for this one), but I need something more compact. Hence, the function in question.
EDIT: The following code is functional and working. Just remember to set the delimiter to $$. Once again, thanks all for the help.
Working Code:
CREATE FUNCTION inrange(
lat1 decimal(11, 7),
long1 decimal(11, 7),
lat2 decimal(11, 7),
long2 decimal(11, 7),
rng decimal(18, 10))
RETURNS INT(1)
BEGIN
DECLARE distance decimal(18, 10);
SET lat1 = lat1 * PI() / 180.0,
long1 = long1 * PI() / 180.0,
lat2 = lat2 * PI() / 180.0,
long2 = long2 * PI() / 180.0;
SET distance = ACOS(SIN(lat1)*SIN(lat2)+COS(lat1)*COS(lat2)*COS(long2-long1))*6371;
IF distance <= rng THEN
RETURN 1;
END IF;
RETURN 0;
END$
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的 RETURNS 和 READS SQL DATA 乱序。 RETURNS 首先:
编辑: 另外,正如您在评论中所说,您引用了数据库中的列。您的函数不仅不知道这些列属于哪个表,而且通常您不应在函数内引用列。相反,向函数添加参数:
然后,在查询中,您可以将列名称传递给函数。
(免责声明:确保我将纬度和经度放在正确的位置。我想我做到了。)
You have your RETURNS and READS SQL DATA out of order. RETURNS comes first:
EDIT: Also, you refer to columns in your database, as you said in your comment. Not only does your function not know to which table those columns belong, but generally, you should not refer to columns from within functions. Instead, add parameters to your function:
Then, in your queries, you can pass the column names to the function.
(Disclaimer: Make sure that I put the lats and longs in the correct places. I think I did.)
从您所包含的过程来看,您的分隔符似乎设置为
$$
。如果是这样,请尝试以下操作:
From the procedure you have included, it looks like your delimiter is set to
$$
.If so, please try the following: