【发布时间】:2013-07-31 13:04:03
【问题描述】:
我正在用 PHP 做一个项目。我有一张带有 LAT 和 LONG 字段的餐厅表。我想获得 10 家距离 USER 小于 5km 的餐厅(特定 cuisine_id)。
我正在使用以下函数来计算距离:
// Function for Getting distance between two coordinates.
function getDistance( $latitude1, $longitude1, $latitude2, $longitude2 ) {
$earth_radius = 6371;
$dLat = deg2rad( $latitude2 - $latitude1 );
$dLon = deg2rad( $longitude2 - $longitude1 );
$a = sin($dLat/2) * sin($dLat/2) + cos(deg2rad($latitude1)) * cos(deg2rad($latitude2)) * sin($dLon/2) * sin($dLon/2);
$c = 2 * asin(sqrt($a));
$d = $earth_radius * $c;
return $d;
}
从数据库中选择记录时是否可以使用该功能?
谁能告诉我一个示例查询,用于获取(使用 LIMIT 值)距离 5 公里 或更短的餐厅(用户 LAT 和 LONG 可用)一口气(如果可能的话,使用上面提到的getDistance 函数)。
【问题讨论】:
-
您可以使用它来生成 mysql 运行的查询,或者运行 mysql 查询然后在结果上使用它。 mysql 无法在查询中运行 php 函数
-
“您要么使用它来生成查询 mysql 运行” - 我该怎么做?你能举个例子吗,因为我是 SQL 新手。
-
考虑到您没有提供数据库的任何详细信息,因此很难给您答案
-
如果您正在使用地理坐标,您可能需要查看spatial extensions。