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 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示