【问题标题】:Codeigniter - How to get records from database where one fields value is more than another?Codeigniter - 如何从一个字段值大于另一个字段值的数据库中获取记录?
【发布时间】:2012-05-08 11:26:33
【问题描述】:

在我的数据库中,我有两列名为“amount_raised”和“funding_goal”。

我想从我的数据库中获取“amount_raised”等于或大于“funding_goal”的所有记录。

我正在使用 Codeigniters 活动记录。这是我目前所拥有的:

    function get_recently_successful($limit, $offset){

        $data = '';

        $this->db->order_by('date','desc');
        $this->db->where('published', '1'); 
        $this->db->where('amount_raised >=', 'funding_goal'); 
        $query = $this->db->limit($limit, $offset)->get('projects'); 

        foreach ($query->result() as $row) {
            $data[] = array(
                'id' => $row->id,
                'date' => $row->date,
                'project_title' => $row->project_title,

            );
        }

        return $data;

    }

上面的代码只是返回数据库中的所有值。不是我在哪里指定的。我怎样才能让它工作??

【问题讨论】:

    标签: mysql codeigniter activerecord


    【解决方案1】:

    试试这个。

    $this->db->where('amount_raised >= funding_goal');
    

    现在您通过查询发送值“funding_goal”,从而使其成为:
    WHERE amount_raised >= 'funding_goal'

    您希望它与列而不是字符串进行比较:
    WHERE amount_raised >= funding_goal

    您始终可以通过插入以下内容对查询进行故障排除:

    echo $this->db->last_query();
    

    $query = 行之后。

    【讨论】:

      【解决方案2】:

      如果您在数组中使用多个条件,您也可以尝试以下方法:

      $this->db->where(array('status' => 'Active', 'category' => $catId));

      你也可以像这样使用另一个条件:

      $this->db->where('amount_raised >=funding_goal');

      你也可以使用last_query()查看sql查询来理解它。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-11-18
        • 2012-12-02
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多