求MYSQL中两点之间的距离。(使用点数据类型)

时间:2022-02-11 15:25:11

Suppose I have a 2 column table like this:

假设我有一个2列的表格

| user_id      | int(11) | NO   | UNI | NULL    |                |
| utm          | point   | NO   | MUL | NULL    |                |

As you can see, it's very simple. utm is a Point data-type. I insert it like this:

正如你所看到的,它非常简单。utm是一个点数据类型。我这样插入:

INSERT INTO mytable(user_id, utm) VALUES(1, PointFromWKB(point(50, 50)));

Then, I create a Spatial index.

然后,我创建一个空间索引。

ALTER TABLE mytable ...add spatial index on(utm) or something. (forgot)

Alright, everything is good. Now , I want to select * where distance < 99999. But it doesn't work!

好了,一切都好。现在,我要选择距离< 99999的*。但它不工作!

//This is supposed to select all where the distance is less than 99999999.
set @mypoint = PointFromWKB(point(20,20))
select * from mytable where GLength(LineString(utm, @mypoint)) < 9999999;
Empty set (0.00 sec)
select * from mytable where GLength(LineStringFromWKB(LineString(utm, @mypoint))) < 9999;
Empty set (0.00 sec)

By the way, I have tried to INSERT INTO without the PointFromWKB...and it didn't work...that's why someone suggested that PointFromWKB to me.

顺便说一下,我尝试在没有PointFromWKB的情况下插入……和它没有工作…这就是为什么有人向我提出这个观点的原因。

3 个解决方案

#1


3  

Solved. This is what I did:

解决了。这就是我所做的:

where GLength(LineStringFromWKB(LineString(asbinary(utm), asbinary(@mypoint)))) < 9999999999999;

#2


1  

You can also do it this way. Not sure if it's faster or not.

你也可以这样做。不确定是不是更快。

select * from mytable where glength(geomfromtext(concat('linestring(', x(utm), ' ', y(utm), ',20 20', ')'))) < 99999999

#3


0  

As far as I know, u have to try this way-

据我所知,你必须试试这种方法。

select * from mytable
   where 
    (
        GLength(
          LineStringFromWKB(
            LineString(
              geoPoint, 
              GeomFromText('POINT(51.5177 -0.0968)')
            )
          )
        )
      ) < 99999999

More in this answer.

这个答案。

#1


3  

Solved. This is what I did:

解决了。这就是我所做的:

where GLength(LineStringFromWKB(LineString(asbinary(utm), asbinary(@mypoint)))) < 9999999999999;

#2


1  

You can also do it this way. Not sure if it's faster or not.

你也可以这样做。不确定是不是更快。

select * from mytable where glength(geomfromtext(concat('linestring(', x(utm), ' ', y(utm), ',20 20', ')'))) < 99999999

#3


0  

As far as I know, u have to try this way-

据我所知,你必须试试这种方法。

select * from mytable
   where 
    (
        GLength(
          LineStringFromWKB(
            LineString(
              geoPoint, 
              GeomFromText('POINT(51.5177 -0.0968)')
            )
          )
        )
      ) < 99999999

More in this answer.

这个答案。