使用 ZF 1.12.4-dev、PHP 5.3.26、MySQL 5.6.13 测试
Zend_Db_Table::setDefaultAdapter($adapter);
$table = new Zend_Db_Table("TableName");
$startLat = 39.0;
$startLng = 122.0;
$select = $table->select()
->from($table, array("latitude", "longitude",
"distance" => "SQRT(
POW(69.1 * (latitude - $startLat), 2) +
POW(69.1 * ($startLng - longitude) * COS(latitude / 57.3), 2))"))
->having("`distance` < ?", 25)
->order("distance");
$rowset = $table->fetchAll($select);
print_r($rowset->toArray());
使用 ZF 2.2.5-dev、PHP 5.3.26、MySQL 5.6.13 测试
use Zend\Db\TableGateway\TableGateway;
use Zend\Db\Sql\Select;
use Zend\Db\Sql\Expression;
$table = new TableGateway("TableName", $adapter);
$startLat = 39.0;
$startLng = 122.0;
$rowset = $table->select(
function (Select $select) {
global $startLat, $startLng;
$select->columns(
array(
"latitude",
"longitude",
"distance" => new Expression("SQRT(
POW(69.1 * (latitude - ?), 2) +
POW(69.1 * (? - longitude) * COS(latitude / 57.3), 2))",
array($startLat, $startLng))
)
);
$select->having->lessThan("distance", 25);
$select->order("distance");
}
);
print_r($rowset->toArray());
测试数据和测试SQL查询(不带PHP):
use test;
drop table if exists TableName;
create table TableName (
id int auto_increment primary key,
latitude numeric(9,4) NOT NULL,
longitude numeric(9,4) NOT NULL,
key (latitude,longitude)
);
insert into TableName (latitude, longitude) values
(10.0, 10.0),
(20.0, 20.0),
(30.0, 30.0),
(40.0, 40.0),
(50.0, 50.0),
(60.0, 60.0),
(70.0, 70.0),
(39.2, 122.2);
set @startlat = 39.0;
set @startlng = 122.0;
SELECT latitude, longitude, SQRT(
POW(69.1 * (latitude - @startlat), 2) +
POW(69.1 * (@startlng - longitude) * COS(latitude / 57.3), 2)) AS distance
FROM TableName
HAVING distance < 25
ORDER BY distance;
SQL 测试的输出:
$ mysql -E < 19057187.sql
*************************** 1. row ***************************
latitude: 39.2000
longitude: 122.2000
distance: 17.484284526375415
PHP测试的输出:
$ php 19057187.php
Array
(
[0] => Array
(
[latitude] => 39.2000
[longitude] => 122.2000
[distance] => 17.484284526375415
)
)
Array
(
[0] => Array
(
[latitude] => 39.2000
[longitude] => 122.2000
[distance] => 17.48428452637566
)
)