【问题标题】:Combining `where` and `like` statements by using the CI activerecords使用 CI 活动记录组合 `where` 和 `like` 语句
【发布时间】:2012-05-08 03:12:15
【问题描述】:

长话短说:有没有可能,如果有 - 我如何构建一个看起来有点像这个的查询

SELECT * FROM a
    WHERE row = 1 AND 
    (other_row LIKE '%..%' OR another_row LIKE '%..%')

基本上我无法提出/找到解决此问题的方法。我似乎无法弄清楚如何将括号添加到 activerecords 查询中。这可能吗?

我当前的代码:

$data = $this->where('row', 1)
    ->like('another_row', '...')        
    ->or_where('row', 1)
    ->like('other_row', $search)                
    ->get('a')
    ->result();

结果:

SELECT * FROM (`a`) WHERE `row` = 1 OR `row` = 1 AND `another_row` LIKE '%...%' AND other_row` LIKE '%...%'

【问题讨论】:

    标签: php sql codeigniter


    【解决方案1】:

    你可以试试这个。

       $query = $this->db->select('*')
                ->from('a')
                ->where('row',1)
                ->where("(other_row LIKE '%%' OR another_row LIKE '%%' )")
                ->get();
    
    
        foreach ($query->result() as $row) {
            //do sth.
        }
    

    您可以编写自定义查询字符串(来自活动记录类)

     Custom string:
     You can write your own clauses manually:
    
     $where = "name='Joe' AND status='boss' OR status='active'";
    
     $this->db->where($where);
    

    【讨论】:

      【解决方案2】:

      我也遇到or_where 的问题,所以我只是让我的自定义查询像

      $this->db->query("SELECT * FROM `a` WHERE `row`=1 AND (CONDITION1 OR CONDITION2)")
      

      【讨论】:

        【解决方案3】:
        $sql= "SELECT * FROM `a`
        WHERE row = '1' AND 
        (other_row LIKE '%" . $value . "%' OR another_row LIKE '%" . $value . "%')";
         $this->db->query($sql);
        

        【讨论】:

          【解决方案4】:

          你为什么不试试这个:

              $this->db->where("row = 1 AND 
                  (other_row LIKE '%..%' OR another_row LIKE '%..%')");
          $this->db->get('a');
          

          这是您在 CI 中编写自定义 WHERE 的方法。如果不是您要的,请随时解释

          【讨论】:

            【解决方案5】:

            您可以使用以下内容:

            $this->db->like('title', 'match', 'before');
            

            它将产生:

            WHERE `title` LIKE '%match' ESCAPE '!'
            

            【讨论】:

              【解决方案6】:
              $this->db->where('salary_range_min >= ',$salarymin)
              $this->db->where('salary_range_max <= ',$salarymax)
              $this->db->where('job_title like '.$title.'% or skill like %'.$title.'%');
              // OR */
              $this->db->where('job_title like '.$title.'% or skill like %'.$title.'%',FALSE);
              // OR */
              $this->db->where('job_title like '.$title.'% or skill like %'.$title.'%',NULL);
              

              试试这个

              【讨论】:

                猜你喜欢
                • 2016-05-21
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2020-09-27
                相关资源
                最近更新 更多