if语句在mysql中查询codeigniter

时间:2014-06-12 09:04:56

标签: mysql codeigniter

我必须执行查询检查变量是否为空。像:

if($symbol == ""){
    $data_array = $this->db->select('*')
                       ->select_max('date')
                    ->get('company')
                ->row_array();                  
}else{
    $data_array = $this->db->select('*')
                   ->select_max('date')
               ->where('symbol',$symbol)
               ->get('company')
               ->row_array();

    }

如何在单个查询中创建它?

1 个答案:

答案 0 :(得分:2)

我认为你可以这样做:

if($symbol == ""){
    $where  = "symbol != 0"; ##$where  = "symbol != ''"; whichever suits your case
}else{
    $where  = "symbol = '".$symbol."'";
}

$data_array = $this->db->select('*')
                    ->select_max('date')
                    ->where($where, false, false)
                    ->get('company')
                    ->row_array();