or_where 在 codeigniter 模型中不工作
or_where is not working in codeigniter model
我有一个查询 model.But 第二个 or_where 不工作
型号
$this->db->select('st_student.st_id');
$this->db->from('st_student');
$this->db->where('st_status',1);
$this->db->or_where('st_status',2);
if(($from!='') && ($to!='')){
$this->db->where("ab_date BETWEEN '$from' AND '$to'");
$this->db->or_where("as_date BETWEEN '$from' AND '$to'");
}
$this->db->group_by('st_student.st_id');
$result=$this->db->get();
sql查询
SELECT `st_student`.`st_id`
FROM (`st_student`)
WHERE `st_status` = 1
OR `st_status` = 2
AND `ab_date` BETWEEN '01/15/2016' AND '01/26/2016'
AND `as_date` BETWEEN '01/15/2016' AND '01/26/2016'
GROUP BY `st_student`.`st_id`
那有什么问题
您没有得到预期的结果,因为查询没有条件括号。
尝试如下:
$status_condition = 'st_status = 1 or st_status = 2';
$this->db->select('st_student.st_id');
$this->db->from('st_student');
$this->db->where($status_condition);
if(($from!='') && ($to!=''))
{
$date_condition = "((ab_date BETWEEN '".$from."' AND '".$to."') or (as_date BETWEEN '".$from."' AND '".$to."'))";
$this->db->where($date_condition);
}
$this->db->group_by('st_student.st_id');
$result=$this->db->get();
使用
$this->db->where("st_status = 1 or st_status = 2")
而不是
$this->db->where('st_status',1);
$this->db->or_where('st_status',2);
试试这个:
$this->db->select('st_student.st_id');
$this->db->from('st_student');
$this->db->where('st_status',1, FALSE);
$this->db->or_where('st_status',2, NULL, FALSE);
if(($from!='') && ($to!='')){
$this->db->where("ab_date BETWEEN '$from' AND '$to'", FALSE);
$this->db->or_where("as_date BETWEEN '$from' AND '$to'", NULL, FALSE);
}
$this->db->group_by('st_student.st_id');
$result=$this->db->get();
使用or_where_in
$this->db->select('st_student.st_id');
$this->db->from('st_student');
$this->db->where('st_status',1);
$this->db->or_where('st_status',2);
if((!empty($from)) && (!empty($to)))
{
$this->db->where('ab_date BETWEEN date("$from", "Y/m/d H:i:s") AND date("$to", "Y/m/d H:i:s") ');
$this->db->or_where_in('as_date BETWEEN date("$from", "Y/m/d H:i:s") AND date("$to", "Y/m/d H:i:s") ');
}
$this->db->group_by('st_student.st_id');
$query = $this->db->get();
$result = $query->result_array();
我有一个查询 model.But 第二个 or_where 不工作
型号
$this->db->select('st_student.st_id');
$this->db->from('st_student');
$this->db->where('st_status',1);
$this->db->or_where('st_status',2);
if(($from!='') && ($to!='')){
$this->db->where("ab_date BETWEEN '$from' AND '$to'");
$this->db->or_where("as_date BETWEEN '$from' AND '$to'");
}
$this->db->group_by('st_student.st_id');
$result=$this->db->get();
sql查询
SELECT `st_student`.`st_id`
FROM (`st_student`)
WHERE `st_status` = 1
OR `st_status` = 2
AND `ab_date` BETWEEN '01/15/2016' AND '01/26/2016'
AND `as_date` BETWEEN '01/15/2016' AND '01/26/2016'
GROUP BY `st_student`.`st_id`
那有什么问题
您没有得到预期的结果,因为查询没有条件括号。
尝试如下:
$status_condition = 'st_status = 1 or st_status = 2';
$this->db->select('st_student.st_id');
$this->db->from('st_student');
$this->db->where($status_condition);
if(($from!='') && ($to!=''))
{
$date_condition = "((ab_date BETWEEN '".$from."' AND '".$to."') or (as_date BETWEEN '".$from."' AND '".$to."'))";
$this->db->where($date_condition);
}
$this->db->group_by('st_student.st_id');
$result=$this->db->get();
使用
$this->db->where("st_status = 1 or st_status = 2")
而不是
$this->db->where('st_status',1);
$this->db->or_where('st_status',2);
试试这个:
$this->db->select('st_student.st_id');
$this->db->from('st_student');
$this->db->where('st_status',1, FALSE);
$this->db->or_where('st_status',2, NULL, FALSE);
if(($from!='') && ($to!='')){
$this->db->where("ab_date BETWEEN '$from' AND '$to'", FALSE);
$this->db->or_where("as_date BETWEEN '$from' AND '$to'", NULL, FALSE);
}
$this->db->group_by('st_student.st_id');
$result=$this->db->get();
使用or_where_in
$this->db->select('st_student.st_id');
$this->db->from('st_student');
$this->db->where('st_status',1);
$this->db->or_where('st_status',2);
if((!empty($from)) && (!empty($to)))
{
$this->db->where('ab_date BETWEEN date("$from", "Y/m/d H:i:s") AND date("$to", "Y/m/d H:i:s") ');
$this->db->or_where_in('as_date BETWEEN date("$from", "Y/m/d H:i:s") AND date("$to", "Y/m/d H:i:s") ');
}
$this->db->group_by('st_student.st_id');
$query = $this->db->get();
$result = $query->result_array();