Codeigniter - 如何从数据库中获取一个字段值多于另一个字段的记录?

时间:2012-05-08 11:26:33

标签: mysql codeigniter activerecord

在我的数据库中,我有两列名为'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;

    }

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

2 个答案:

答案 0 :(得分:3)

试试这个。

$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 =行之后。

答案 1 :(得分:0)

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

  

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

您也可以使用其他条件:

  

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

您可以使用last_query()来查看sql查询以了解它。