当点被硬编码且没有错误或警告时,此代码选择表中的所有行:
SELECT *
FROM lat_lng
WHERE Contains(
GeomFromText('POLYGON((0 0,0 100,100 100,100 0,0 0))'),
GeomFromText('Point(50 50)') )
OR 由变量 $var = "50 50"定义(没有错误或警告)
SELECT *
FROM lat_lng
WHERE Contains(
GeomFromText('POLYGON((0 0,0 100,100 100,100 0,0 0))'),
GeomFromText('Point($var)') )
但是,当我使用名为“位置”的列来定义点时,会选择零行(没有错误或警告):
SELECT *
FROM lat_lng
WHERE Contains(
GeomFromText('POLYGON((0 0,0 100,100 100,100 0,0 0))'),
GeomFromText('Point(location)') )
基于这个两行示例表:
id | location
1 | 50 50
2 | 500 500
为什么?
最佳答案
您提供的字符串是 Point(location)
(逐字)。您想要的是引用位置列,因此您需要构建字符串:
CONCAT('Point(',location,')')
您可能还想将 Point
直接存储在另一个表中(而不是文本),然后在不使用 GeomFromText
更新
将 Point
直接存储在您的数据库中:
架构:
CREATE TABLE locations (id integer primary key auto_increment,location point);
INSERT INTO locations (location) VALUES (Point(50,50));
INSERT INTO locations (location) VALUES (Point(500,500));
要求:
SELECT id
FROM locations
WHERE Contains(
GeomFromText('POLYGON((0 0,0 100,100 100,100 0,0 0))'),
location);
更新 2
SQL fiddle :http://sqlfiddle.com/#!2/b5d1f/9
关于mysql - 检查多边形中的点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25340664/