Codeigniter喜欢,or_like不适用于哪里

Sak*_*ota 4 php mysql codeigniter where-clause sql-like

我在codeigniter应用程序中发现了一个问题.我用4种不同的和or_like编写了一个简单的搜索查询.

$q = $this->db->select('*')
->from('table')
->like('col', $search_string1, both)
->or_like('col', $search_string2, both)
->or_like('col', $search_string3,  both)
->or_like('col', $search_string4, both)
->get()->results
Run Code Online (Sandbox Code Playgroud)

并且它可以按照我的意愿工作,但我决定在我的表中添加名为"active"的新col,并且我只想显示活动的行= 1.所以我尝试添加查询的位置,但它没有工作正如我所料.

$q = $this->db->select('*')
->from('table')
->where('active', 1)
->like('col', $search_string1, both)
->or_like('col', $search_string2, both)
->or_like('col', $search_string3,  both)
->or_like('col', $search_string4, both)
->get()->results
Run Code Online (Sandbox Code Playgroud)

此查询还显示活动设置为0的行.如何修复此行以仅在codeigniter中显示active = 1的行?

Ros*_*hal 7

您好,请使用以下代码

$q = $this->db->select('*')
->from('table')
->where('active', 1)
->where("(col LIKE '%".$search_string1."%' OR col LIKE '%".$search_string2."%' OR col LIKE '%".$search_string3."%' OR col LIKE '%".$search_string4."%')", NULL, FALSE)
->get()->results;
Run Code Online (Sandbox Code Playgroud)

谢谢


mic*_*czy 7

您也可以使用查询分组

代码如下所示:

$this->db->where('active', 1);
$this->db->group_start();
$this->db->or_like('col', $search_string1, both)
$this->db->or_like('col', $search_string2, both)
$this->db->or_like('col', $search_string3,  both)
$this->db->or_like('col', $search_string4, both)
$this->db->group_end();

$q = $this->db->get('table')->result();
Run Code Online (Sandbox Code Playgroud)

使用 group_start 和 group_end 确保 then 之间的这部分 or_like 语句将进入括号。


小智 5

有关@ Roshan代码的解释,因为对于在CI中学习SQL的人来说,答案可能并不明显.(我还修复了代码中的错误.)

$q = $this->db
              // ->select('*') // you don't need select('*') if you want everything
              // ->from('table') // Don't need this either
              ->where('active', 1)
              ->where("(col LIKE '%".$search_string1."%' OR col LIKE '%".$search_string2."%' OR col LIKE '%".$search_string3."%' OR col LIKE '%".$search_string4."%')", NULL, FALSE)
              ->get('table') // add your table name here
              ->result();  // it's not "->results"
Run Code Online (Sandbox Code Playgroud)

所以你的最终查询看起来像这样:

$q = $this->db->where('active', 1)
              ->where("(col LIKE '%".$search_string1."%' OR col LIKE '%".$search_string2."%' OR col LIKE '%".$search_string3."%' OR col LIKE '%".$search_string4."%')", NULL, FALSE)
              ->get('table')
              ->result(); 
Run Code Online (Sandbox Code Playgroud)

以下是上述问题:

"get all results from 'table' 
    where active = 1 AND `col` = search_string1
 OR where active = 1 AND `col` = search_string2
 OR where active = 1 AND `col` = search_string3
 OR where active = 1 AND `col` = search_string4

 Then assign the result object to $q
Run Code Online (Sandbox Code Playgroud)

查看它的一种更简单的方法,但编写代码的方式更复杂(但可能更容易理解):

$q = $this->db
            // where active=1 AND col LIKE %$search_string1%
             ->where('active', 1)
             ->like('col', $search_string1, 'both')

            // OR where active=1 AND col LIKE %$search_string2%
             ->or_where('active', 1)
             ->like('col', $search_string2, 'both')

             // OR where active=1 AND col LIKE %$search_string3%
             ->or_where('active', 1)
             ->like('col', $search_string3, 'both')

             // OR where active=1 AND col LIKE %$search_string4%
             ->or_where('active', 1)
             ->like('col', $search_string4, 'both')

            ->get('table')
            ->result();
Run Code Online (Sandbox Code Playgroud)

有时候你需要这样做.例如,如果active可能是几个州. on,off,pending你需要检查该字段为特定状态.在你的情况下没有必要像active始终为1,但如果active 成为你想多件事情,你必须拼出来在你的查询生成器.