MYSQL:从传递2列的表中选择值:st_distance_sphere

时间:2019-12-16 09:58:45

标签: mysql sql

我发现了这个 Stack Overflow 问题。

Distance in meters between two Spacial Points in MySQL query

我想为我的用例实现它,但出现此错误:

  

1210-#1210 st_distance_sphere的参数错误。

我有一个包含这些列的数据库表:

  • CompanyObjectId
  • CompanyName
  • CityName
  • 纬度
  • 经度
  • CampingLocationType
  • PhotoUrl

如何从Latitude表中的每一行中提取Longitudetbl_cc并计算出以米为单位的距离小于或等于300米的结果?

SELECT CompanyObjectId, CompanyName, CityName, Latitude, Longitude,
       CampingLocationType, PhotoUrl
FROM tbl_cc
WHERE st_distance_sphere(POINT(13.5145461,43.564368), POINT(Longitude, Latitude)) <= 300

我也尝试执行此查询,但是出现了相同的错误:

SELECT * FROM (SELECT CompanyObjectId, CompanyName, CityName, Latitude, Longitude,
       CampingLocationType, PhotoUrl, (st_distance_sphere(POINT(13.5145461,43.564368), POINT(Longitude, Latitude))) AS MetriDaPosizione
FROM tbl_camper_contact) AS Q WHERE MetriDaPosizione <= 300

****更新****

现在我在php中执行脚本的地方会生成此查询sql,其中包括仪表:

SELECT CompanyObjectId, CompanyName, CityName, Latitude, Longitude,
                  CampingLocationType, PhotoUrl, st_distance_sphere(point(13.5129324,43.5578855), point(CAST(Longitude AS DECIMAL(18,14)), CAST(Latitude AS DECIMAL(18,14))), 100000) as Metri
                  FROM tbl_camper_contact
            WHERE st_distance_sphere(point(13.5129324,43.5578855), point(CAST(Longitude AS DECIMAL(18,14)), CAST(Latitude AS DECIMAL(18,14))), 100000)

在mysql admin中它可以正常工作,但是在php中,我收到使我发疯的错误:

  

2019年12月16日星期一格林尼治标准时间[错误] [客户端93.36.55.43]-   www.testcolan.it-AH01215:PHP警告:mysqli_query():( HY000 / 1210):   的st_distance_sphere参数不正确   /web/htdocs/www.testcolan.it/home/campit/rest_api/sql/DBOperations.php   在759行上:   /web/htdocs/www.testcolan.it/home/campit/rest_api/getListaDoveSostare.php   2019年12月16日星期一16:14:26 GMT [错误] [客户端93.36.55.43]-   www.testcolan.it-AH01215:PHP警告:mysqli_fetch_array()预期   参数1为mysqli_result,布尔值在   /web/htdocs/www.testcolan.it/home/campit/rest_api/GeneralFunctions.php   在第242行:   /web/htdocs/www.testcolan.it/home/campit/rest_api/getListaDoveSostare.php

我的脚本php是:

public function GetListaDoveSostare($lat, $lng, $kmmin, $ordinamentoData, $localita)
{
  $metri = (intval($kmmin) * 1000);

  $com = new DbConnect();
  $sql = "";


    $sql = "SELECT CompanyObjectId, CompanyName, CityName, Latitude, Longitude,
                  CampingLocationType, PhotoUrl, st_distance_sphere(point(".floatval($lng).",".floatval($lat)."), point(CAST(Longitude AS DECIMAL(18,14)), CAST(Latitude AS DECIMAL(18,14))), $metri) as Metri
                  FROM tbl_camper_contact
            WHERE st_distance_sphere(point(".floatval($lng).",".floatval($lat)."), point(CAST(Longitude AS DECIMAL(18,14)), CAST(Latitude AS DECIMAL(18,14))), $metri)";

  mysqli_set_charset($com->getDb(), 'utf8');
  $result = mysqli_query($com->getDb(), $sql);
  return $result;
}

然后将返回值放入生成json的函数中

 public function GetListaDoveSostare($resultDoveSostare, $lat, $lng)
    {
      $dbOperationsObject = new DBOperations();
      $doveSostare= array();

        while ($row = mysqli_fetch_array($resultDoveSostare)) {

          $dove_sostare = array(
            "CompanyObjectId" => $row['CompanyObjectId'],
            "CompanyName" => $row['CompanyName'],
            "CityName" => $row['CityName'],
            "Latitude" => $row['Latitude'],
            "Longitude" => $row['Longitude'],
            "CampingLocationType" => ($row['CampingLocationType'] == "Camperplace") ? "Area Sosta" : "Campeggio",
            "PhotoUrl" => $row['PhotoUrl'],
            "MetriDaPosizioneCorrente" =>  $row['Metri']//$metri

          );

          $json= json_encode($dove_sostare);
          $dove_sostare = json_decode($json);
          array_push($doveSostare, $dove_sostare);
      }




      return $doveSostare;

    } 

非常感谢您的帮助
克里斯蒂安

1 个答案:

答案 0 :(得分:0)

在您的第一个查询中,您没有正确传递半径:

SELECT CompanyObjectId, CompanyName, CityName, Latitude, Longitude,
       CampingLocationType, PhotoUrl
FROM tbl_cc
WHERE st_distance_sphere(POINT(13.5145461,43.564368), POINT(Longitude, Latitude)) <= 300

应该是

SELECT CompanyObjectId, CompanyName, CityName, Latitude, Longitude,
       CampingLocationType, PhotoUrl
FROM tbl_cc
WHERE st_distance_sphere(POINT(13.5145461,43.564368), POINT(Longitude, Latitude), 300)

您提到的错误会引发“如果存在radius参数但不是正数”。 请参阅this以获取更多参考信息