doudansui6650 2018-07-12 08:46
浏览 231
已采纳

MYSQL:在地理空间查询中使用VARCHAR字段作为POINT

I've decided to detect distance between two points. One of the points is static -35.735019 51.410041, however, the other one is a point which is given by a database field.

Here is my code:

SELECT
  r0_.restaurant_point AS restaurant_point_0,
  ST_Distance(GeomFromText('POINT(35.735019 51.410041)'), GeomFromText('POINT(r0_.restaurant_point)')) *
  100 AS sclr_1
FROM restaurants r0_

We stored r0.restaurant_point as a VARCHAR. Take the string below as an example: 35.73062161548168 51.410930156707764

Although it would be worked if I change r0.restaurant_point with static value, it can't get measure with database field.

Is there anyway for binding this value or another way to resolve?

  • 写回答

1条回答 默认 最新

  • dtla92562 2018-07-12 10:02
    关注

    To concatenate the value of the column restaurant_point in the string that represents the point, you can use CONCAT().

    GeomFromText(CONCAT('POINT(', t.restaurant_point, ')'))
    

    In your case you are just trying to create a Point NOT from the coordinates in the column, BUT from the column name itself. you need to change your code as follows:

    SELECT
    r0_.restaurant_point AS restaurant_point_0,
    ST_Distance(
      GeomFromText('POINT(35.735019 51.410041)'), 
      GeomFromText(CONCAT('POINT(', r0_.restaurant_point, ')'))
    ) * 100 AS sclr_1
    FROM restaurants r0_
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置