我有以下活动记录查询。
//Example 1
public function info($school, $class, $student, $keyword)
{
$this->db->where('school.id', $school);
$this->db->where('class.id', $class);
$this->db->where('student.id', $student);
$this->db->or_where('school.description', $keyword);
$this->db->or_where('class.description', $keyword);
$this->db->or_where('student.description', $keyword);
return $this->db->get('info')->result();
}
我想将底部的 3 个“or_where”语句分组,以便它们包含在前 3 个“where”语句中。我想出的解决方案是这样的......
//Example 2
public function info($school, $class, $student, $keyword)
{
$this->db->where('school.description', $keyword);
$this->db->where('school.id', $school);
$this->db->where('class.id', $class);
$this->db->where('student.id', $student);
$this->db->or_where('class.description', $keyword);
$this->db->where('school.id', $school);
$this->db->where('class.id', $class);
$this->db->where('student.id', $student);
$this->db->or_where('student.description', $keyword);
$this->db->where('school.id', $school);
$this->db->where('class.id', $class);
$this->db->where('student.id', $student);
return $this->db->get('info')->result();
}
这很好用,但是有没有办法在不重复代码的情况下做到这一点?