dongtao1262 2015-01-31 18:10
浏览 73
已采纳

使用hasrsine公式的MySQL地理空间搜索在同一点上返回null

I'm trying to implement a geospacial search in a php application. Currently, I'm using the following query to find points within 10km of a given latitude and longitude:

SELECT * FROM (
      SELECT *,
             (6378.1 * ACOS(
                  COS(RADIANS(48.856614)) * COS(RADIANS(latitude))
                * COS(RADIANS(2.3522219) - RADIANS(longitude))
                + SIN(RADIANS(48.856614))
                * SIN(RADIANS(latitude)))
             ) AS distance
      FROM `destinations_info`
      WHERE latitude BETWEEN 48.856614  - (10 / 69)
               AND 48.856614  + (10 / 69)
        AND longitude BETWEEN 2.3522219 - (10 / (69 * COS(RADIANS(48.856614))))
               AND 2.3522219 + (10 / (69 * COS(RADIANS(48.856614))))
      ) d
WHERE distance <= 10
LIMIT 1

This works fine as long I don't search for the exact latitude and longitude that is stored into the POI's table.

So for example if I have in my poi table the following entry

id     name     latitude    longitude
1      Paris    48.856614   2.3522219000000177

And I call the query with lat = 48.856614 and long = 2.3522219000000177 I won't get any results. As far as I see this happens because the following operation returns NULL and not 0 in mysql:

SELECT (6378.1 * acos(cos(radians(48.856614)) * cos(radians(48.856614)) * cos( radians(2.3522219) - radians(2.3522219)) + sin(radians(48.856614)) * sin(radians(48.856614)))) 

Running the same in Chrome Calculator I get 0 rad.

Is there something wrong in the query or do I need to include the NULL results as well using "OR IS NULL"

  • 写回答

2条回答 默认 最新

  • dongmen5867 2016-04-01 04:33
    关注

    IFNULL( ... ),0) AS Distance will also help you to get around the error.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?