MySQL - 几何

MySQL - GEOMETRY

我有两张桌子。一个是点,另一个是多边形。我怎样才能找到哪个多边形内的哪个点?

你只是把 ST_CONTAINS() 中的参数放错了,第一个参数是多边形,第二个是点,因为 ST_CONTAINS(g1,g2) 将 return 1 如果 g1 完全包含 g2 或 return 0.

SELECT messagespoints.coordinates FROM messagespoints 
WHERE T_CONTAINS( (SELECT geofencespolygon.coordinates 
FROM geofencespolygon),messagespoints.coordinates)

您还可以在 on clause 中使用 ST_Contains() 连接两个表,如下所示:

SELECT messagespoints.coordinates 
FROM geofencespolygon
inner join messagespoints 
on ST_CONTAINS(geofencespolygon.coordinates,messagespoints.coordinates)

DB-Fiddle:

 Schema and insert statements:

 create table testPoly(polID int, pol polygon);

 create table testPoint(pntID int, pnt point);
 insert into testPoint values(1,ST_GeomFromText('POINT(2 1)')); 
 insert into testPoint values(2,ST_GeomFromText('POINT(5 3)')); 
 insert into testPoint values(3,ST_GeomFromText('POINT(5 10)')); 

 INSERT INTO testPoly VALUES (1,ST_GeomFromText('POLYGON((0 0, 8 0, 4 4, 0 0))'));

查询:

 select polID,pntID
 FROM testPoly 
 inner join testPoint
 on ST_Contains(pol, pnt)

输出:

polID pntID
1 1

db<>fiddle here