【问题标题】:Codeigniter like, or_like doesn't work with whereCodeigniter like, or_like 不适用于 where
【发布时间】:2016-12-13 04:35:02
【问题描述】:

我在我的 codeigniter 应用中发现了一个问题。我使用 4 种不同的 like 和 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

它可以按我的意愿工作,但我决定在我的表中添加名为“活动”的新列,我只想显示活动 = 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

此查询还显示将活动设置为 0 的行。如何解决此问题以在 codeigniter 中仅显示活动 = 1 的行?

【问题讨论】:

  • 条件应该在like..之后的位置

标签: php mysql codeigniter where-clause sql-like


【解决方案1】:

你好,请使用以下代码

$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;

谢谢

【讨论】:

    【解决方案2】:

    对于@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"
    

    所以您的最终查询将如下所示:

    $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(); 
    

    这是上面的要求:

    "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
    

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

    $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();
    

    有时您需要这样做。例如,如果active 可能是多个状态。 onoffpending 并且您需要检查该字段以了解特定状态。在您的情况下不是必需的,因为 active 始终为 1,但如果 active 可能不仅仅是您想要的,您必须在查询生成器中将其拼写出来。

    【讨论】:

      【解决方案3】:

      你也可以使用query grouping

      代码将如下所示:

      $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();
      

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

      【讨论】:

        【解决方案4】:

        如果您即将使用数据表服务器端处理,您可能还会遇到此问题,但这是解决方法。

        if(!empty($search_val)) {
                $x=0;
                $this->db->group_start();
                foreach($columns as $s_col) {
                    if($x==0) {
                        $this->db->like($s_col,$search_val);
                    }else {
                        $this->db->or_like($s_col,$search_val);
                    }
                    $x++;
                }
                $this->db->group_end();               
            }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2020-10-05
          • 1970-01-01
          • 2013-04-19
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多