使用具有地理空间扩展的MySQL 5.7,我有一个表,该表具有字段“ poly”,该字段定义了光束的横向边界

我有一个字段“ pt”,它定义了一个位置。

为了确定给定光束中是否存在给定点,我运行查询:

set @p=(select pt from beams where name='current_position');
SET @a = (select poly from beams where name='first_beam');
SELECT MBRWithin(@p,@a);


如果点在多边形内,则返回“ 1”,否则返回“ 0”

但是我有大约1000条光束,我想找到current_position所在的光束,我是否必须运行一千次查询,还是有办法优雅地返回其所在的光束?光束不重叠,最多只有1个匹配项。

最佳答案

MBRWithin可以在JOIN中使用

SELECT p.id, a.id FROM
    (select pt from beams where name='current_position') as p
INNER JOIN
    (select poly from beams where name='first_beam') as a
ON MBRWithin(p.pt, a.poly)

关于mysql - 需要一种优雅的方法来查找数百个匹配项而无需执行数百个查询,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39706464/

10-09 00:34
查看更多