Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
“where子句”codeigniter中的未知列“距离”_Codeigniter_Activerecord - Fatal编程技术网

“where子句”codeigniter中的未知列“距离”

“where子句”codeigniter中的未知列“距离”,codeigniter,activerecord,Codeigniter,Activerecord,你好,朋友,下面是我正在执行的查询,获取数据库错误未知列距离,请帮助 $this->db->select("post.*,(3956 * 2 * ASIN(SQRT( POWER(SIN(($cur_lat -abs(post.post_lat)) * pi()/180 / 2),2) + COS($cur_lat * pi()/180) * COS(abs(post.post_lat) * pi()/180)* POWER(SIN(($cur_long - post.post_l

你好,朋友,下面是我正在执行的查询,获取数据库错误未知列距离,请帮助

$this->db->select("post.*,(3956 * 2 * ASIN(SQRT( POWER(SIN(($cur_lat -abs(post.post_lat)) * pi()/180 / 2),2) + COS($cur_lat * pi()/180) * COS(abs(post.post_lat) *  pi()/180)* POWER(SIN(($cur_long - post.post_long) *  pi()/180/2), 2) ))) as distance, source.*, user.firstname, user.lastname");
            $this->db->from('post');
            $this->db->join('user', 'user.id = post.cop_id');
            $this->db->join('source', 'post.source_id = source.id');
            $this->db->where('post.cop_id', $user_id);
            $this->db->where('post.post_lat !=', '');
            $this->db->where('post.post_long !=', '');
            if (!empty($radius)) {
                $this->db->where('distance <=', $radius);
            }
            if (!empty($source_array)) {
                $this->db->where_in('post.source_id', $source_array);
            }

            //$this->db->where('source.id !=', 4);
            $query = $this->db->get();
            echo $this->db->last_query(); 
            //  die();
            return $query->result();

那么,别名字段距离不能用于Where条件。这就是为什么会出现这种错误。您可以使用子查询进行计算,并使用其结果检入Where条件。

您可以使用having子句(可以查看别名)代替Where子句

$this->db->having('distance <=', $radius);

echo$this->db->last_query;的输出是什么?我用having子句代替where解决了我的问题