我允许我的用户选择英里半径数和位置,然后我试图获得在该半径范围内的结果。网上有很多教程,其中有一个似乎有完美的查询
'SELECT id, ( 3959 * acos( cos( radians(37) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians(-122) ) + sin( radians(37) ) * sin( radians( lat ) ) ) ) AS distance FROM markers HAVING distance < 25 ORDER BY distance LIMIT 0 , 20;'
但我正在使用cakephp并且我想使用分页组件,因此我需要将该查询转换为条件数组,该数组选择所有标记和相关数据。谁能帮我吗?我是cakephp的新手,并且在最好的时候对sql绝对没用。
HAVING位特别令人困惑......
修改
我现在正在使用此代码
$this->Paginator->settings = array(
'fields'=>array('id','( 3959 * acos( cos( radians(37) ) * cos(radians( '.$lat.' ) ) * cos( radians( '.$lng.' ) - radians(-122) ) + sin( radians(37) ) * sin(radians( '.$lat.' ) ) ) ) AS distance '),
'limit' => 20,
'conditions'=>array('distance < '.$miles)
);
$data = $this->Paginator->paginate('Job');
$this->set(compact('data'));
至少在运行查询,但我收到错误
Unknown column 'distance' in 'where clause'
有什么想法吗?
更多编辑
日志正在显示
SELECT `Job`.`id`, ( 3959 * acos( cos( radians(37) ) * cos(radians( 54.59728500000001 ) ) * cos( radians( -5.930119999999988 ) - radians(-122) ) + sin( radians(37) ) * sin(radians( 54.59728500000001 ) ) ) ) AS distance FROM `trainnation`.`jobs` AS `Job` LEFT JOIN `trainnation`.`users` AS `User` ON (`Job`.`user_id` = `User`.`id`) LEFT JOIN `trainnation`.`courses` AS `Course` ON (`Job`.`course_id` = `Course`.`id`) WHERE 1 = 1 GROUP BY distance having distance < 49 LIMIT 20
看起来正确,但它现在没有从DB中提取任何东西。这些选项应该至少有9个
答案 0 :(得分:1)
试试这个
$this->paginate = array('fields'=>array('id','( 3959 * acos( cos( radians(37) ) * cos(radians( lat ) ) * cos( radians( lng ) - radians(-122) ) + sin( radians(37) ) * sin(radians( lat ) ) ) ) AS distance '),
'limit' => 20,
'group'=>'distance having distance < 25 ')
);
$marker_data = $this->paginate('Marker');