【问题标题】:Unknown column 'distance' in 'where clause' codeigniter“where子句”代码点火器中的未知列“距离”
【发布时间】:2015-12-14 13:20:56
【问题描述】:

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

$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();

【问题讨论】:

  • echo $this-&gt;db-&gt;last_query(); 的输出是什么?

标签: codeigniter activerecord


【解决方案1】:

除了使用 where() 子句,你可以使用 having() 子句,它可以看到别名。

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

【讨论】:

    【解决方案2】:

    嗯,别名字段“距离”不能在 Where 条件下使用。这就是为什么会出现这个错误。您可以使用子查询进行计算,并使用其结果来检查 Where 条件。

    【讨论】:

    • 我已经通过使用“have”子句而不是“where”解决了我的问题
    猜你喜欢
    • 2016-03-19
    • 2016-05-27
    • 2020-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-03
    相关资源
    最近更新 更多