2013-09-27 21 views
1

我如何轉換這一點:轉換SQL到Zend_Table

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; 

到Zend_Table。

我有兩個鍵,拉特和長在數據庫中,我想要獲取接近用戶的數據,查詢工作由第一個用戶提供,它將其轉換爲Zend_Table,但如果我有來自人的數據是在同一個城市它不返回任何數據,我想查詢有待提高,也顯示了更大的距離,結果

$this->select()->from(array('c' => 'content'))...... 

我NOE知道如何做到這一點,你可以男人幫我?

// LE

$data2->setIntegrityCheck(FALSE) 
     ->from(array('c' => 'content'), array('location', 'id', 'user_id', 'date', 'attachment', 'content','lat','long')) 
     ->columns(array(
      'distance' => "SQRT(POW(69.1 * (lat - {$location['lat']}), 2) + POW(69.1 * ({$location['long']} - long) * COS(lat/57.3), 2))" 
     )) 
     ->having('distance < ?', 25) 

它看起來像查詢是錯誤的查找用戶接近你,這個人應該做的伎倆

ASIN(
SQRT(POWER(SIN((@orig_lat - 
abs( 
dest.lat)) * pi()/180/2),2) + COS(@orig_lat * pi()/180) * COS( 
abs 
(dest.lat) * pi()/180) * POWER(SIN((@orig_lon – dest.lon) * pi()/180/2), 2))) 

as distance 

回答

3

與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 
     ) 

) 
+0

這裏真正的主人,帶有經緯度的鍵需要是雙倍還是浮動?現在我有他們作爲浮號 – Uffo

+0

我試着用這兩種方法......並且它不返回任何數據......並且用戶在數據庫中具有相同的緯度和長度,所以它應該找到它們... – Uffo

+0

我已經添加我上面的測試。首先只是一個SQL測試,以確保查詢的工作。然後輸出ZF1和ZF2碼。 –

1
$db = Zend_Db_Table::getDefaultAdapter(); 
$select = $db->select(); 
$select->from('TableName', array('latitude', 'longitude')); 
$select->columns(array(
    'distance' => "SQRT(POW(69.1 * (latitude - {$startLat}), 2) + POW(69.1 * ({$startLong} - longitude) * COS(latitude/57.3), 2))" 
)); 
$select->having('distance < ?', 25); 
$select->order('distance ASC'); 
+0

PDOException:SQLSTATE [42000]:語法錯誤或訪問衝突:1064您的SQL語法錯誤;檢查與您的MySQL服務器版本相對應的手冊,以便在'long'* COS(lat/57.3),2))AS'distance','ud'.'first_name','ud'.last_name ','在第1行C:\ library \ Zend \ Db \ Statement \ Pdo.php上線228 – Uffo

+0

更新了代碼,看看我做了什麼 – Uffo