到目前为止,我已经能够找到相同半径内的最近点(在此情况下为仓库)。 (已经有很多答案的常见问题)。
这是实际代码(它不使用多个半径)
$radius = 5; // miles
$q = "
SELECT DISTINCT
warehouse_latitude.post_id,
warehouse_longitude.meta_value as longitude,
warehouse_latitude.meta_value as latitude,
((ACOS(SIN($latitude * PI() / 180) * SIN(warehouse_latitude.meta_value * PI() / 180) + COS($latitude * PI() / 180) * COS(warehouse_latitude.meta_value * PI() / 180) * COS(($longitude - warehouse_longitude.meta_value) * PI() / 180)) * 180 / PI()) * 60 * 1.1515) AS distance
FROM {$this->wpdb->postmeta} as warehouse_latitude
LEFT JOIN {$this->wpdb->postmeta} as warehouse_longitude ON warehouse_latitude.post_id = warehouse_longitude.post_id
WHERE warehouse_latitude.meta_key = 'warehouse_latitude' AND warehouse_longitude.meta_key = 'warehouse_longitude'
HAVING distance < $radius
ORDER BY distance ASC
LIMIT 1
";
相反:
我想选择最近的点,但是为每个点定义了半径(仓库)。
因此,如果点B和C在范围内,并且点B最接近,则应选择点B。
wp_postmeta表:
meta_id | post_id | meta_key | meta_value
------------------------------------------------------
324802 | 9714 | warehouse_latitude | 47.1978754
324809 | 9715 | warehouse_latitude | 47.2064462
324814 | 9716 | warehouse_latitude | 47.214434
324803 | 9714 | warehouse_longitude | -1.54441
324810 | 9715 | warehouse_longitude | -1.5461347
324815 | 9716 | warehouse_longitude | -1.565993
324806 | 9714 | warehouse_radius | 5
324811 | 9715 | warehouse_radius | 2
324816 | 9716 | warehouse_radius | 10
架构:
答案 0 :(得分:1)
不是答案。评论太久了...
1 LEFT JOIN warehouse_longitude ON ... -- This is an INNER JOIN because of LINE 3
2 LEFT JOIN warehouse_radius ON ... -- This is also an INNER JOIN, because of LINE 4
3 WHERE warehouse_longitude.meta_key = 'warehouse_longitude'
4 AND warehouse_radius.meta_key = 'warehouse_radius'
因此,您最好将它们写为INNER JOIN。
答案 1 :(得分:0)
因此,这里是解决方案(从上一个查询更新)。
如果您有更有效/更易理解的方式来计算距离,则不要介意再给您答案。
SELECT DISTINCT
warehouse_latitude.post_id,
warehouse_longitude.meta_value as longitude,
warehouse_latitude.meta_value as latitude,
warehouse_radius.meta_value as radius,
((ACOS(SIN($latitude * PI() / 180) * SIN(warehouse_latitude.meta_value * PI() / 180) + COS($latitude * PI() / 180) * COS(warehouse_latitude.meta_value * PI() / 180) * COS(($longitude - warehouse_longitude.meta_value) * PI() / 180)) * 180 / PI()) * 60 * 1.1515) AS distance
FROM {$this->wpdb->postmeta} as warehouse_latitude
LEFT JOIN {$this->wpdb->postmeta} as warehouse_longitude ON warehouse_latitude.post_id = warehouse_longitude.post_id
LEFT JOIN {$this->wpdb->postmeta} as warehouse_radius ON warehouse_latitude.post_id = warehouse_radius.post_id
WHERE warehouse_latitude.meta_key = 'warehouse_latitude' AND warehouse_longitude.meta_key = 'warehouse_longitude' AND warehouse_radius.meta_key = 'warehouse_radius'
HAVING distance < (radius * 0.62)
ORDER BY distance ASC
LIMIT 1
我在半径值上添加了一个连接:
LEFT JOIN {$this->wpdb->postmeta} as warehouse_radius ON warehouse_latitude.post_id = warehouse_radius.post_id
还有WHERE语句中的另一个条件:
AND warehouse_radius.meta_key = 'warehouse_radius'
因此:
我可以将HAVING语句更新为:
HAVING distance < (radius * 0.62) -- Km to Miles