CI框架 where 跟 OR 怎么连用

最近再用CI框架,有时候会懵一下,因为没太多时间看手册,所以用到的自己先总结一下,免得到时候自己还得花时间再看,下面贴代码:

我先用的是

public function get_cands_where( $where, $begin=null, $size=null, $paixu = 'cand_city', $projs = '')
    {
    	if( isset( $begin ) && isset( $size ) ){
    		$this->db->limit( $size, $begin );
    	}
    	$this->db->where( $where );
        if (!empty($projs)) {
            foreach ($projs as $projkey => $projvalue) {
                $this->db->or_where('proj_id', $projvalue['proj_id']);
            }
        }
        $this->db->order_by( $paixu );
    	$query = $this->db->get( $this->table_name );
    	$result = $query->result_array();
    	return $result;
    }

SELECT COUNT(*) AS count FROM `tbl_cand` WHERE `cand_phone` = 2147483647 AND `cand_email` = '[email protected]'  OR  `proj_id` = '100137' OR `proj_id` = '100147'

但我想要的结果是这样:

SELECT COUNT(*) AS count FROM `tbl_cand` WHERE `cand_phone` = 2147483647 AND `cand_email` = '[email protected]'  AND  (`proj_id` = '100137' OR `proj_id` = '100147')

so?加两行就可,很简单

public function get_cands_where( $where, $begin=null, $size=null, $paixu = 'cand_city', $projs = '')
    {
    	if( isset( $begin ) && isset( $size ) ){
    		$this->db->limit( $size, $begin );
    	}
    	$this->db->where( $where );
        if (!empty($projs)) {
            $this->db->group_start();
            foreach ($projs as $projkey => $projvalue) {
                $this->db->or_where('proj_id', $projvalue['proj_id']);
            }
            $this->db->group_end();
        }
        $this->db->order_by( $paixu );
    	$query = $this->db->get( $this->table_name );
    	$result = $query->result_array();
    	return $result;
    }

$this->db->group_start();

$this->db->group_end();

这俩代表后面的分组开始,也就是左括号,end代表结束,就是右括号

省的自己拼接了,毕竟自己拼的没人家框架封装的安全,稳定

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章