首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >合并类where - where_in

合并类where - where_in
EN

Stack Overflow用户
提问于 2016-09-09 00:37:07
回答 1查看 295关注 0票数 2

假设我在CI中有一个模型,返回用户需要的内容。

代码语言:javascript
复制
$find = 'something';
$id_user = Array ( [0] => 1 [1] => 5 [2] => 20 [3] => 21 ) ;

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

代码语言:javascript
复制
    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 () ;
    }

我将error Array转换为string转换;我拥有的输入来自$find$id_users,可以是任何内容。我期望这样的逻辑,给出表PR_DATA中的所有数组,该表在ref_nopr_titlereq_date列上有%$find%,但在req_by_idcheck_by_idapprove_by_id列中只有$id_users *(1、5、20或21)。

有人能帮忙吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-09-09 01:18:33

这个答案假设您需要在代码中的第一个where_in()中插入括号。

不幸的是,CodeIgniter并不完全支持带有活动记录的括号。因此,您必须使用where(),其中包含更复杂的SQL语法。

代码语言:javascript
复制
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的活动记录中,将如何处理语法:

优先,其中将被视为: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的东西

第二次,其中将被视为:AND (ref_no LIKE '%$find%' || pr_title LIKE '%$find%' || req_date LIKE '%$find%')

注意:,我还没有测试代码,因为我没有您的db结构。如果不起作用就告诉我。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39402127

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档