【问题标题】:Subquery calculation error?子查询计算错误?
【发布时间】:2015-05-29 04:49:12
【问题描述】:

为什么这个查询不起作用?

$query = ("SELECT * FROM 
                (SELECT *, (
                (((endingLatitude - " . $h . ")(endingLatitude - " . $h . "))
                /" . pow($r1, 2) . ")
                 + 
                 ((endingLongitude - " . $k . ")(endingLongitude - " . $k . "))
                 /" . pow($r2, 2) . "
                 ) AS point2 
                FROM (SELECT *, ((
                (
                ((startingLatitude - " . $h . ")(startingLatitude - " . $h . "))
                /" . pow($r1, 2) . ") 
                + 
                ((startingLongitude - " . $k . ")(startingLongitude - " . $k . "))
                /" . pow($r2, 2) . ")) 
                AS point1 
                FROM (SELECT * FROM trips WHERE distance >='" . ($distance * .25) . "') as query1)
                 as query2) as query3 WHERE point1 <= 1 AND point2 <= 1 LIMIT 0 , 10;");

$result = mysqli_query($con, $query);

$h 和 $k 分别是椭圆的 x 和 y 坐标。我正在使用找到here 的公式来计算两个点 (startingLat,startingLong) 和 (endingLat,endingLong) 是否在垂直高度为 $r1 和水平高度为 $r2 的椭圆内。我还将搜索的行限制为距离单元格值大于 $distance * .25 的行。

我认为这可能与括号错误有关,或者与我子查询/执行计算的方式有关。

使用

die(mysqli_error($con)); 

返回错误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 '(endingLatitude - 36.9564753)) /796.842964388) + ((endingLongitud' at line 3

【问题讨论】:

  • 你遇到了什么错误?
  • @anantkumarsingh 它失败了if(!$result) 然后打印,$result = mysqli_query($con, $query);
  • 输入$result = mysqli_query($con, $query) or die(mysqli_error($con)); 并检查你得到的真正错误。谢谢。
  • @anantkumarsingh 对此感到抱歉,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 '(endingLatitude - 36.9564753)) /796.842964388) + ((endingLongitud' at line 3
  • 请添加您的代码。不要作为评论。谢谢

标签: php mysql mysqli in-subquery


【解决方案1】:

相信你一定要用Mysql的乘法算术运算符,*。 https://dev.mysql.com/doc/refman/5.0/en/arithmetic-functions.html#operator_times

代替:

(endingLatitude - " . $h . ")(endingLatitude - " . $h . ")

这样做...

(endingLatitude - " . $h . ") * (endingLatitude - " . $h . ")

【讨论】:

  • 这可能是它!修复了错误,但是,我的查询仍然无法正常运行,所以它必须是别的东西......我会调查它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-21
  • 2022-01-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多