gpt4 book ai didi

php - CodeIgniter Active Record 多个 "where"和 "or"语句

转载 作者:可可西里 更新时间:2023-11-01 12:31:23 25 4
gpt4 key购买 nike

我有以下 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, $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();
}

这很好用,但是有没有办法不用重复代码就可以做到这一点?

最佳答案

我找到了解决方案!

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->where("(school.description LIKE '$keywords' OR class.description LIKE '$keywords' OR student.description LIKE '$keywords')");

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

关于php - CodeIgniter Active Record 多个 "where"和 "or"语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18926025/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com