微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

php-使用Codeigniter活动记录进行“距离内”查询

我正在尝试使用ActiveRecord在CI中创建以下查询

SELECT *, 
   ( 3959 * acos( cos( radians($lat) ) 
   * cos( radians( lat ) ) 
   * cos( radians( lng ) - radians($lng) ) 
   + sin( radians($lat) ) 
   * sin( radians( lat ) ) ) ) AS distance 
FROM locations 
HAVING distance <= $miles 
ORDER BY distance 
LIMIT 0, 20

我试过了

$where = "( 3959 * acos( cos( radians($lat) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians($lng) ) + sin( radians($lat) ) * sin( radians( lat ) ) ) ) AS distance FROM locations";        
$this->db->where($where);                           
$this->db->having('distance <= ' . $miles);                     
$this->db->order_by('distance');                    
$this->db->limit(20, 0);

问题(我认为)是我已经在告诉我我是通过$query = $this-> db-> get(‘locations’);从哪个表获取数据的.在我模型的结尾所以我收到以下错误

A Database Error Occurred Error
Number: 1064

You have an error in your sql Syntax;
check the manual that corresponds to
your MysqL server version for the
right Syntax to use near ‘AS distance
FROM user_profiles HAVING distance
<= 100 ORDER BY distance LIMI’ at
line 5

SELECT * FROM (locations) WHERE
country = ‘US’ AND tags = ‘technology’ AND
( 3959 * acos( cos(
radians(25.9331488) ) * cos( radians(
lat ) ) * cos( radians( lng ) -
radians(-80.1625463) ) + sin(
radians(25.9331488) ) * sin( radians(
lat ) ) ) ) AS distance FROM
locations HAVING
distance<= 100
ORDER BY
distance` LIMIT 20

Filename:
C:\wamp\www\mysite\system\database\DB_driver.PHP

Line Number: 330

一些注意事项..我在模型中使用了几个where()函数.距离查询应与其他子句共存.

解决方法:

您正在sql中将距离计算设置为WHERE.您需要在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);

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐