加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – CodeIgniter Active记录多个“where”和“or”语句

发布时间:2020-12-13 13:32:07 所属栏目:PHP教程 来源:网络整理
导读:我有以下Active Record查询. //Example 1public 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.descript
我有以下Active Record查询.
//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,$keyword) 
{
    $this->db->where('school.description',$keyword);
    $this->db->where('school.id',$student);

    $this->db->or_where('class.description',$student);

    $this->db->or_where('student.description',$keyword); 
    $this->db->where('school.id',$student);

    return $this->db->get('info')->result();
}

这工作正常,但是有没有办法在不重复代码的情况下执行此操作?

我找到了解决方案!
public function info($school,$student);

    $this->db->where("(school.description LIKE '$keywords' OR class.description LIKE '$keywords' OR student.description LIKE '$keywords')");

    return $this->db->get('info')->result();
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读