【发布时间】:2016-02-09 21:49:55
【问题描述】:
我有一个查询要从给定坐标中找到最近的纬度/经度:
public function findClosestByLatitudeLongitude($latitude, $longitude, $distanceUnit = 111.045, $radius = 150)
$stmt = $this->db->prepare('SELECT
f.fcst_latitude,
f.fcst_longitude,
f.fcst_resolution,
:distance_unit * DEGREES(
ACOS(
COS(
RADIANS(:latitude)
) * COS(
RADIANS(f.fcst_latitude)
) * COS(
RADIANS(:longitude) - RADIANS(f.fcst_longitude)
) + SIN(
RADIANS(:latitude)
) * SIN(
RADIANS(f.fcst_latitude)
)
)
) AS distance
FROM t_fcst_data_coord AS f
WHERE
f.fcst_latitude BETWEEN :latitude - (:radius / :distance_unit)
AND :latitude + (:radius / :distance_unit)
AND f.fcst_longitude BETWEEN :longitude - (
:radius / (
:distance_unit * COS(
RADIANS(:latitude)
)
)
)
AND :longitude + (
:radius / (
:distance_unit * COS(
RADIANS(:latitude)
)
)
)
ORDER BY distance ASC
LIMIT 100
');
结果是一个按距离排序的数组,包含预测的分辨率,如下所示:
(
[0] => Array
(
[fcst_latitude] => 46.295396
[fcst_longitude] => 6.854558
[fcst_resolution] => 9.0
[distance] => 1.2113482186062683
)
[1] => Array
(
[fcst_latitude] => 46.313622
[fcst_longitude] => 6.843681
[fcst_resolution] => 3.0
[distance] => 1.4198633375521186
)
[2] => Array
(
[fcst_latitude] => 46.314401
[fcst_longitude] => 6.884638
[fcst_resolution] => 3.0
[distance] => 2.213273758077741
)
[3] => Array
(
[fcst_latitude] => 46.285180
[fcst_longitude] => 6.844827
[fcst_resolution] => 3.0
[distance] => 2.5347004607874783
)
[...] => Array
(
[fcst_latitude] => ...
[fcst_longitude] => ...
[fcst_resolution] => ...
[distance] => ...
)
[53] => Array
(
[fcst_latitude] => 46.199091
[fcst_longitude] => 6.886765
[fcst_resolution] => 27.0
[distance] => 12.064028782357124
)
[...] => Array
(
[fcst_latitude] => ...
[fcst_longitude] => ...
[fcst_resolution] => ...
[distance] => ...
)
)
我怎样才能得到只显示唯一分辨率的结果,按分辨率排列的最小距离顺序? 预期结果是:
(
[0] => Array
(
[fcst_latitude] => 46.199091
[fcst_longitude] => 6.886765
[fcst_resolution] => 27.0
[distance] => 12.064028782357124
)
[1] => Array
(
[fcst_latitude] => 46.295396
[fcst_longitude] => 6.854558
[fcst_resolution] => 9.0
[distance] => 1.2113482186062683
)
[2] => Array
(
[fcst_latitude] => 46.313622
[fcst_longitude] => 6.843681
[fcst_resolution] => 3.0
[distance] => 1.4198633375521186
)
)
我尝试 GROUP BY fcst_resolution 并选择 MIN 距离,但结果是一个经纬度错误的数组:
(
[0] => Array
(
[fcst_latitude] => 44.972113
[fcst_longitude] => 8.737022
[fcst_resolution] => 9.0
[distance] => 1.2113482186062683
)
[1] => Array
(
[fcst_latitude] => 45.231748
[fcst_longitude] => 5.680505
[fcst_resolution] => 3.0
[distance] => 1.4198633375521186
)
[2] => Array
(
[fcst_latitude] => 45.118703
[fcst_longitude] => 8.640296
[fcst_resolution] => 27.0
[distance] => 12.064028782357124
)
)
谢谢
【问题讨论】:
标签: mysql group-by coordinates distance