【问题标题】:I just got an Error Syntax near in a query我刚刚在查询中得到了一个错误语法
【发布时间】:2019-12-04 12:37:38
【问题描述】:

我在 MySQL 查询中有一个错误,但我不知道它是什么

错误是

ERROR 1064 (42000):您的 SQL 语法有错误;检查 与您的 MySQL 服务器版本相对应的手册 在 'sin( 弧度(30.9006547) ) * sin( 弧度(lat) 附近使用的语法 itude) ) ) ,8) 作为距离 FROM ' 在第 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;

【问题讨论】:

  • 您引用的查询中没有31...
  • 我知道我只是把它拿出来你的意思是纬度和经度
  • 我的数据库中有一个 geo_distance_km() 函数。它节省了很多打字。只是在说'。此外,很明显,罪的左侧缺少某些东西
  • 你能告诉我是否可以吗?以及如何使用它?

标签: mysql mysql-error-1064


【解决方案1】:

在您的代码中,您错过了 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;

【讨论】:

  • 查询中仍然出错 (1064):第 10 行 '( radians(31.50546547) ) * sin( radians(latitude) ) ) ,8)
  • answer update ....... where 条件中缺少相同的 + 符号 .. 所以在适当的地方添加 +
  • 如果您看起来更好,您会发现错误消息不一样,第二个显示代码的不同部分(对于相同的错误代码)
【解决方案2】:

这是一个计算地球上距离的函数示例,以公里为单位。如果你喜欢它,就用它吧……别忘了在构造函数之前和之后改变你的分隔符……

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 太陌生了我想我缺乏 MySQL 基础知识,如果我要使用它,如果用户活跃并获得批准,我可以得到吗?就像我在上面发布的查询一样
  • 是的,您只需调用该函数来代替所有那些 ROUND(...acos...lat..) 东西。
  • 谢谢!我会弄明白的
猜你喜欢
  • 1970-01-01
  • 2023-01-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多