合并哪里 - 喜欢 - where_in

Combine where - like - where_in

假设我在 CI 中有一个模型返回用户需要..

$find = 'something';
$id_user = Array ( [0] => 1 [1] => 5 [2] => 20 [3] => 21 ) ;

所以,我必须把它放在这里,但出了点问题..

    public function find_pr_by_user ($find,$id_users) {
        $this -> db -> where_in ('req_by_id'.$id_users || 'check_by_id',$id_users || 'approve_by_id',$id_users);
        $this -> db -> where ("(ref_no LIKE '%$find%' || pr_title LIKE '%$find%' || req_date LIKE '%$find%')");
        $query = $this -> db -> get ('pr_data') ;
        return $query -> result () ;
    }

我收到数组到字符串转换错误;我的输入来自 $find$id_users,可以是任何东西。我期待这个逻辑,给我所有来自 table PR_DATA 的数组,它在 ref_nopr_titlereq_date 列上有 %$find% 但只有$id_users *(1 或 5 或 20 或 21) 在列 req_by_idcheck_by_idapprove_by_id.

有人能帮忙吗?

此答案假设您需要在代码的第一个 where_in() 中使用括号。

不幸的是,CodeIgniter 不完全支持 Active Record 的括号。因此,您将不得不使用 where() 和更复杂的 SQL 语法。

public function find_pr_by_user ($find,$id_users) {
    $id_users_glued = implode(",",$id_users);
    $this->db->where('(req_by_id IN (' . $id_users_glued . ') || check_by_id IN (' . $id_users_glued . ') || approve_by_id IN (' . $id_users_glued . '))');
    $this->db->where("(ref_no LIKE '%$find%' || pr_title LIKE '%$find%' || req_date LIKE '%$find%')");
    $query = $this->db->get('pr_data') ;
    return $query->result () ;
}

在CI的Active Record中,语法是这样处理的:

FIRST WHERE 将被视为:WHERE (req_by_id IN ($id_users_glued) || check_by_id IN ($id_users_glued) || approve_by_id IN ($id_users_glued)

$id_users_glued 会产生类似 1,2,3,4,5

的结果

SECOND WHERE 将被视为:AND (ref_no LIKE '%$find%' || pr_title LIKE '%$find%' || req_date LIKE '%$find%')

注意:我没有测试代码,因为我没有你的数据库结构。如果它不起作用,请告诉我。