我刚刚在查询中遇到错误语法
I just got an Error Syntax near in a query
我在 MySQL 查询中有一个错误,但我不知道是什么
错误是
ERROR 1064 (42000): 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 'sin( radians(30.9006547) ) * sin( radians(lat
itude) ) ) ,8) as distance FROM ' at line 3
查询是
SELECT h.*
, ROUND(1.609344 * 3956 * acos( cos( radians(30.9006547) ) * cos( radians(latitude) ) *
cos( radians(longitude) - radians(30.8524007) ) sin( radians(30.9006547) ) * sin( radians(latitude) ) ) ,8) as distance
FROM helper h
where is_available = 1
and is_active = 1
and is_approved = 1
and ROUND((1.609344 * 3956 * acos( cos( radians(30.9006547) ) * cos( radians(latitude) ) *
cos( radians(longitude) - radians(30.8524007) )sin( radians(30.9006547) ) * sin( radians(latitude) ) ) ) ,8) <= 60
order
by distance LIMIT 3;
在你的代码中你错过了 cos() 和 sin() 之间的 +
符号
SELECT h.*
, ROUND(1.609344 * 3956 * acos( cos( radians(30.9006547) ) * cos( radians(latitude) ) *
cos( radians(longitude) - radians(30.8524007) ) + /*<<<<<< here */
sin( radians(30.9006547) ) * sin( radians(latitude) ) ) ,8) as distance
FROM helper h
where is_available = 1
and is_active = 1
and is_approved = 1
and ROUND((1.609344 * 3956 * acos( cos( radians(30.9006547) ) * cos( radians(latitude) ) *
cos( radians(longitude) - radians(30.8524007) ) + /* <<<< and here */
sin( radians(30.9006547) ) * sin( radians(latitude) ) ) ) ,8) <= 60
order
by distance LIMIT 3;
下面是一个计算地球距离(以公里为单位)的函数示例。如果你喜欢它,使用它......并且不要忘记在构造函数之前和之后更改你的分隔符......
CREATE DEFINER=`root`@`localhost` FUNCTION `geo_distance_km`(lat1 double, lon1 double, lat2 double, lon2 double) RETURNS double
begin
declare R int DEFAULT 6372.8;
declare phi1 double;
declare phi2 double;
declare d_phi double;
declare d_lambda double;
declare a double;
declare c double;
declare d double;
set phi1 = radians(lat1);
set phi2 = radians(lat2);
set d_phi = radians(lat2-lat1);
set d_lambda = radians(lon2-lon1);
set a = sin(d_phi/2) * sin(d_phi/2) +
cos(phi1) * cos(phi2) *
sin(d_lambda/2) * sin(d_lambda/2);
set c = 2 * atan2(sqrt(a), sqrt(1-a));
set d = R * c;
return d;
end
我在 MySQL 查询中有一个错误,但我不知道是什么
错误是
ERROR 1064 (42000): 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 'sin( radians(30.9006547) ) * sin( radians(lat itude) ) ) ,8) as distance FROM ' at line 3
查询是
SELECT h.*
, ROUND(1.609344 * 3956 * acos( cos( radians(30.9006547) ) * cos( radians(latitude) ) *
cos( radians(longitude) - radians(30.8524007) ) sin( radians(30.9006547) ) * sin( radians(latitude) ) ) ,8) as distance
FROM helper h
where is_available = 1
and is_active = 1
and is_approved = 1
and ROUND((1.609344 * 3956 * acos( cos( radians(30.9006547) ) * cos( radians(latitude) ) *
cos( radians(longitude) - radians(30.8524007) )sin( radians(30.9006547) ) * sin( radians(latitude) ) ) ) ,8) <= 60
order
by distance LIMIT 3;
在你的代码中你错过了 cos() 和 sin() 之间的 +
符号
SELECT h.*
, ROUND(1.609344 * 3956 * acos( cos( radians(30.9006547) ) * cos( radians(latitude) ) *
cos( radians(longitude) - radians(30.8524007) ) + /*<<<<<< here */
sin( radians(30.9006547) ) * sin( radians(latitude) ) ) ,8) as distance
FROM helper h
where is_available = 1
and is_active = 1
and is_approved = 1
and ROUND((1.609344 * 3956 * acos( cos( radians(30.9006547) ) * cos( radians(latitude) ) *
cos( radians(longitude) - radians(30.8524007) ) + /* <<<< and here */
sin( radians(30.9006547) ) * sin( radians(latitude) ) ) ) ,8) <= 60
order
by distance LIMIT 3;
下面是一个计算地球距离(以公里为单位)的函数示例。如果你喜欢它,使用它......并且不要忘记在构造函数之前和之后更改你的分隔符......
CREATE DEFINER=`root`@`localhost` FUNCTION `geo_distance_km`(lat1 double, lon1 double, lat2 double, lon2 double) RETURNS double
begin
declare R int DEFAULT 6372.8;
declare phi1 double;
declare phi2 double;
declare d_phi double;
declare d_lambda double;
declare a double;
declare c double;
declare d double;
set phi1 = radians(lat1);
set phi2 = radians(lat2);
set d_phi = radians(lat2-lat1);
set d_lambda = radians(lon2-lon1);
set a = sin(d_phi/2) * sin(d_phi/2) +
cos(phi1) * cos(phi2) *
sin(d_lambda/2) * sin(d_lambda/2);
set c = 2 * atan2(sqrt(a), sqrt(1-a));
set d = R * c;
return d;
end