Codeigniter活动记录:大于语句

时间:2010-11-09 14:32:51

标签: php activerecord codeigniter

我正在尝试将“大于”where语句转换为CI的Active Record语法。当我使用这个片段

    $this->db->join('product_stocks', "product_stocks.size_id_fk = product_attributes.id", "left");
    $this->db->where('product_stocks.stock_level', '> 1');      
    $result = $this->db->get('product_attributes')->result_array();

然后打印$ this-> db-> last_query();显示WHERE product_stocks . stock_level = '> 1',这当然不正确。可以这样做吗?

4 个答案:

答案 0 :(得分:59)

我认为这应该可以解决问题:

$this->db->where('product_stocks.stock_level >', '1');  //moved the >

答案 1 :(得分:6)

$this->db->where('product_stocks.stock_level >', '1');
$this->db->where(array('product_stocks.stock_level >'=>'1'));
应该这样做。注意字段名称和运算符之间的空格;否则你最终会得到错误1064.

答案 2 :(得分:3)

您还可以将String参数传递给where()函数,如下所示,

$this->db->where('product_stocks.stock_level > 1');

答案 3 :(得分:0)

我想在CI中有以下内容:

 $sQuery = "SELECT auftrag, saal, DATE_FORMAT(konzertdatum,'%e, %M, %Y') AS konzertdatum2 FROM auftrag 
    JOIN saal on auftrag.saal_id = saal.id 
    WHERE konzertdatum < NOW() + INTERVAL 240 DAY AND auftrag like '%$sWord%' order by konzertdatum asc LIMIT 4";
 $aOrder = $this->db->query($sQuery);
 $aOrder = $aOrder->result();

没有CI就可以正常工作,但是当我使用

      $this->db->select("auftrag, saal, DATE_FORMAT(konzertdatum,'%e, %M, %Y') AS konzertdatum2", false );
    $this->db->from('auftrag');
    $this->db->join('saal', 'auftrag.saal_id = saal.id');
    $this->db->like('auftrag', $sWord);
    $this->db->where('konzertdatum <', 'NOW() + Interval 240 day');

    $this->db->order_by('konzertdatum');
    $this->db->limit(4);

    $oQuery = $this->db->get();

    $aOrder = $oQuery->result();
    print_r($this->db->last_query());

它返回所有不关心where的结果(尽管sql似乎很好):

SELECT auftrag, saal, DATE_FORMAT(konzertdatum, '%e, %M, %Y') AS konzertdatum2 FROM (`auftrag`) JOIN `saal` ON `auftrag`.`saal_id` = `saal`.`id` WHERE `konzertdatum` < 'NOW() + Interval 240 day' AND `auftrag` LIKE '%spangenberg%' ORDER BY `konzertdatum` LIMIT 4