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

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了php – CodeIgniter Active记录多个“where”和“or”语句脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
我有以下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();
}

脚本宝典总结

以上是脚本宝典为你收集整理的php – CodeIgniter Active记录多个“where”和“or”语句全部内容,希望文章能够帮你解决php – CodeIgniter Active记录多个“where”和“or”语句所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。