我有这个查询在phpMyAdmin
中有效SELECT *,
( 6371 * acos ( cos ( radians(21.161908) )
* cos( radians( `latitude` ) )
* cos( radians( `longitude` ) - radians(-86.85152790000001) )
+ sin ( radians(21.161908) )
* sin( radians( `latitude` ) ) ) ) AS distance
FROM _results
HAVING distance < 30
ORDER BY distance
LIMIT 0 , 20
我尝试在CodeIgniter中执行此操作:
$this->select("* , ( 6371 * acos ( cos ( radians(21.161908) ) * cos( radians(latitude) ) * cos( radians( longitude ) - radians(-86.85152790000001) ) + sin ( radians(21.161908) ) * sin( radians( latitude ) ) ) ) AS distance ");
$this->db->having('distance < 30');
$this->db->order_by('distance');
$this->db->limit(20,0);
但是有了这个,我得到了这个错误:
错误号码:1064 您的SQL语法错误...用户附近&#39; *,(6371 * acos(cos(弧度(21.161908))* cos(弧度(纬度))
任何人都有这个错误的想法吗?
注意:之前我意识到之前的查询。
答案 0 :(得分:0)
为什么不尝试这样
$this->db->query("SELECT *,
( 6371 * acos ( cos ( radians(21.161908) )
* cos( radians( `latitude` ) )
* cos( radians( `longitude` ) - radians(-86.85152790000001) )
+ sin ( radians(21.161908) )
* sin( radians( `latitude` ) ) ) ) AS distance
FROM _results
HAVING distance < 30
ORDER BY distance
LIMIT 0 , 20");
答案 1 :(得分:0)
您应该使用$this->db->select()
代替$this->select()
。
我有类似这样的类型查询
$this->db->select("*, ( 3959 * acos( cos( radians($lat) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians($lng) ) + sin( radians($lat) ) * sin( radians( lat ) ) ) ) AS distance");
$this->db->having('distance <= ' . $miles);
$this->db->order_by('distance');
$this->db->limit(20, 0);
工作正常。
答案 2 :(得分:0)
对我来说完美无缺。
$this->db->select("SELECT *,ACOS( SIN( RADIANS( `latitude` ) ) * SIN( RADIANS( $Lat ) ) + COS( RADIANS( `latitude` ) )* COS( RADIANS( $Lat )) * COS( RADIANS( `longitude` ) - RADIANS( $Long )) ) * 6380 AS `distance`");
$this->db->from('_results');
$this->db->where("ACOS( SIN( RADIANS( `latitude` ) ) * SIN( RADIANS( $fLat ) ) + COS( RADIANS( `latitude` ) )* COS( RADIANS( $fLat )) * COS( RADIANS( `longitude` ) - RADIANS( $fLon )) ) * 6380 < 5");
$this->db->orderby('distance');
$this->db->limit(20,0);
return $this->db->get()->result();
答案 3 :(得分:0)
function lat_long($lat,$long){
$query = $this->db->query("SELECT *, ( 6371 * acos ( cos ( radians($lat) ) * cos( radians( `latitude` ) ) * cos( radians( `longitude` ) - radians($long) ) + sin ( radians($lat) ) * sin( radians( `latitude` ) ) ) ) AS distance FROM table HAVING distance < 30 ORDER BY distance LIMIT 0 , 20");
return $query->result();
}