SQL查询和计算;正确格式化查询以仅选择ID

时间:2015-04-15 21:31:51

标签: mysql sql gps haversine

我有一个查询,选择GPS点30英里范围内的所有位置。我希望获得这些地点的身份,但它也会从中心返回距离。

无论如何都要进行距离计算而不返回它?

查询:

SELECT id, 3956 * 2 * ASIN(SQRT(
  POWER(SIN((34.1844709 - abs(dest.lat)) * pi()/180 / 2),
  2) + COS(37.7749295 * pi()/180 ) * COS(abs(dest.lat) *
  pi()/180) * POWER(SIN((-118.131809 - dest.lng) *
  pi()/180 / 2), 2) )) as distance
FROM location dest
having distance < 30
ORDER by distance
LIMIT 30

输出:

---------------------------
id    |    distance   
---------------------------
 1    |    2.310
 2    |    2.356
 17   |    4.298

基于查询: http://www.notaires.fr/sites/default/files/geo_searchjkkjkj_0.pdf

1 个答案:

答案 0 :(得分:5)

你能再做一次选择吗?

Select id
From (SELECT id, 3956 * 2 * ASIN(SQRT(
  POWER(SIN((34.1844709 - abs(dest.lat)) * pi()/180 / 2),
  2) + COS(37.7749295 * pi()/180 ) * COS(abs(dest.lat) *
  pi()/180) * POWER(SIN((-118.131809 - dest.lng) *
  pi()/180 / 2), 2) )) as distance
FROM location dest
having distance < 30
ORDER by distance
LIMIT 30) dst