【发布时间】:2011-09-30 00:54:03
【问题描述】:
我正在尝试使用 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'); 告诉我从哪个表获取数据。所以我收到以下错误:
发生数据库错误错误 编号:1064
您的 SQL 语法有错误; 检查对应的手册 您的 MySQL 服务器版本 在 'AS 距离附近使用的正确语法 来自 user_profiles 有
distancedistanceLIMI' 订购 第 5 行选择 * 从 (
locations) 哪里country= 'US' ANDtags= '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 HAVINGdistance<= 100 ORDER BYdistance` LIMIT 20文件名: C:\wamp\www\mysite\system\database\DB_driver.php
行号:330
一些需要注意的事情.. 我在我的模型中使用了一些where() 函数。距离查询应与其他子句共存。
【问题讨论】:
标签: php mysql codeigniter map