Codeigniter Active record: greater than statement

放肆的年华 提交于 2019-12-12 07:25:52

问题


I'm trying to convert a "greater than" where statement to CI's Active Record syntax. When I use this snippet

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

Then print $this->db->last_query(); shows WHEREproduct_stocks.stock_level= '> 1' which is of course not correct. Can this be done?


回答1:


I think this should do the trick:

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



回答2:


Either

$this->db->where('product_stocks.stock_level >', '1');
or
$this->db->where(array('product_stocks.stock_level >'=>'1'));
should do it. Note the space between the field name and the operator; otherwise you will end up getting Error 1064.


回答3:


You Can also Pass a String parameter to where() function like this,

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



回答4:


I would like to have in CI the following:

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

It works fine without CI, but when I use

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

it returns all results not caring about the where (although the sql seems fine):

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 


来源:https://stackoverflow.com/questions/4134799/codeigniter-active-record-greater-than-statement

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!