结合关键字的代码点火器搜索查询


Codeigniter search query combining keywords

我希望搜索查询返回与所有关键字匹配的结果,但我只能让它匹配任何匹配字符串

即"蛋糕巧克力"要么返回标签字段中带有"蛋糕"或"巧克力"的所有记录,要么返回带有确切标签"蛋糕巧克力"

的结果,而不是我想要的,即获取任何带有"蛋糕"和"巧克力"的记录。这是我得到的:

$key = $this->input->post('searchTerm');
         if($key != ''){
        // if I comment out the next 10 lines, it becomes MATCH-ANY
           $this->db->or_like('product_name',$key);
           $this->db->or_like('product_code',$key);
           $this->db->or_like('description',$key);
           $this->db->or_like('season',$key);
           $this->db->or_like('year',$key);
           $this->db->or_like('photo_style',$key);
           $this->db->or_like('photo_status',$key);
           $this->db->or_like('extra_field1',$key);
           $this->db->or_like('extra_field2',$key);
           $this->db->or_like('additional_notes',$key); 
        // But if I comment out the next 7 lines instead, I match only the entire string.
        $Singlequry = "select  * from (select *,    concat_ws(' ',product_name,product_code,description,season,year,photo_style,photo_status,extra_field1,extra_field2,additional_notes) merged from records )temp where "; 
        $keywordsMany = explode(' ',$key);
        $tempQuery = array();
        foreach($keywordsMany as $each){
        $tempQuery[] = " temp.merged like '%".mysql_real_escape_string($each)."%'";
        }
        $Singlequry = $Singlequry.implode(' or ',$tempQuery);        // end of search type comment-out   
           $makeQuery = true;
         }

万一不明显,我真的不知道我在做什么。我扔了where_in和其他东西来代替or_like但没有运气。综上所述,我需要一点手把手:)

不幸的是,

您无法使用代码点火器or_like生成此查询;您当前的代码生成如下所示的查询

SELECT * from tablename WhHERE product_name like '%key%' or product_code like '%key%' OR..............

它将仅返回与密钥匹配的任何内容。

但是我想你想要所有的比赛,应该是这样的

SELECT * from tablename WhHERE product_name like '%key%' AND product_code like '%key%' AND..............

如果是这样,您可以将or_like替换为如下所示的where

 $this->db->where("product_name LIKE '%$key%'");
 $this->db->where("product_code LIKE '%$key%'");
 $this->db->where("description LIKE '%$key%'");

它将生成预期的查询。它将返回与键全部匹配的结果。
您也可以通过其他方式执行此操作。
希望它能帮助你。