我有一个包含事件和使用MySQL POINT(X,Y) as geocodedLocation
的位置的表,我试图对此数据使用半径搜索现在我已经找到了应该在这里得到的答案
https://gis.stackexchange.com/questions/31628/find-points-within-a-distance-using-mysql。我以前见过这个,显然这是谷歌使用地点服务的答案
我有2个领域应该举办活动
Area 1 = `Array ( [0] => 51.524074 [1] => -0.1005592 )`
Area 2 = `Array ( [0] => 53.818391 [1] => -1.4714507 )`
我有一些活动
Event 1 `geocodedLocation` = POINT(-1.54907739999999, 53.8007554)
Event 2 `geocodedLocation` = POINT(-0.100559200000021, 51.524074)
是的,我知道POINTs是错误的方式,但我没有建立这个,我只需要使用它。
现在这是我正在运行的查询
SELECT a.*
,(
3959 * acos(cos(radians(51.524074)) * cos(radians((
SELECT Y(`geocodedLocation`)
))) * cos(radians((
SELECT X(`geocodedLocation`)
)) - radians(- 0.1005592)) + sin(radians(51.524074)) * sin(radians((
SELECT Y(`geocodedLocation`)
))))
) AS distance
,COUNT(*) AS pagedAmount
FROM Activities a
HAVING distance < 10
ORDER BY a.startTime ASC
正如您所看到我使用位置Area 1
,事件Event 2
位于此位置只是更准确一些但由于某种原因查询不提供Event 2
的结果{1}}
答案 0 :(得分:0)
不确定你想要计算什么,如果它的事件总数那么你必须将一个子查询放入你的选择,但如果你想计算返回的行数,那就有一个php查询就像
$row_cnt = mysqli_num_rows($result);
如果你在一个查询中使用和计数,你还需要包含group by子句以获得更多结果
SELECT a.*
,(3959 * acos(cos(radians(51.524074)) * cos(radians((
SELECT Y(`geocodedLocation`)
))) * cos(radians((
SELECT X(`geocodedLocation`)
)) - radians(- 0.1005592)) + sin(radians(51.524074)) * sin(radians((
SELECT Y(`geocodedLocation`)
))))
) AS distance
,COUNT(*) AS pagedAmount
FROM Activities a
GROUP BY distance
HAVING distance < 200
ORDER BY a.startTime ASC;
修改强>
在mysql中挖掘了一点,发现点对象会将纬度和经度存储为Point(lon, lat)
所以我上面的原始订单给你的距离合适!
答案 1 :(得分:0)
我的一个项目中有类似的查询。根据您的具体情况进行修改,应该是:
SELECT a.*, ( 3959 *
acos( cos( radians(51.524074) ) *
cos( radians( (SELECT Y(`geocodedLocation`)) ) ) *
cos( radians( (SELECT X(`geocodedLocation`)) ) -
radians(-0.1005592) ) + sin( radians(51.524074) ) *
sin( radians( (SELECT Y(`geocodedLocation`)) ) ) ) ) AS distance
FROM Activities a HAVING distance < 10 ORDER BY a.startTime