如何使用查询生成器获取几乎与 sql 列中的单词匹配的单词
how to get a word matching almost like a word in column in sql using query builder
我在使用 codeigniter 构建的网站中有一个搜索框,我在模式中使用查询构建器来获取用户在搜索框中输入的数据,查询如下所示:
public function fetchsearch($limit, $start, $query) {
$this->db->select('*');
$this->db->join('link','link.category=category.category_name');
if($query != "")
{
$this->db->like('link.link_name',$query,'both');
$this->db->or_like('category.category_name',$query,'both');
$this->db->or_like('link.keyword',$query,'both');
}
$this->db->limit($limit, $start);
$query = $this->db->get('category');
// $query = $this->db->query($sql);
return $query->result();
}
这里的问题是,当查询搜索确切的词时,如果没有匹配则不会显示任何结果,而我希望查询搜索的地方,比如用户输入“musec”,查询应该匹配“音乐”。意味着查询应该只在单词中寻找近似匹配。
任何人都可以告诉我如何完成此操作,在此先感谢。
这是我在基于 codeigniter 的所有代码项目中使用的 solution/code:
public function get_search_count($q)
{
$qarray = explode(' ', $q);
$this->db->distinct();
$this->db->select('*');
$this->db->from('products');
$this->db->like('product_name', $q, 'both');
if (count($qarray) > 1) {
foreach ($qarray as $s) {
$this->db->or_like('product_seo_title', $s, 'both');
$this->db->or_like('product_seo_description', $s, 'both');
$this->db->or_like('product_seo_keyword', $s, 'both');
}
} else {
$this->db->or_like('product_seo_title', $q, 'both');
$this->db->or_like('product_seo_description', $q, 'both');
$this->db->or_like('product_seo_keyword', $q, 'both');
}
$query = $this->db->get();
return $query->results();
}
我在使用 codeigniter 构建的网站中有一个搜索框,我在模式中使用查询构建器来获取用户在搜索框中输入的数据,查询如下所示:
public function fetchsearch($limit, $start, $query) {
$this->db->select('*');
$this->db->join('link','link.category=category.category_name');
if($query != "")
{
$this->db->like('link.link_name',$query,'both');
$this->db->or_like('category.category_name',$query,'both');
$this->db->or_like('link.keyword',$query,'both');
}
$this->db->limit($limit, $start);
$query = $this->db->get('category');
// $query = $this->db->query($sql);
return $query->result();
}
这里的问题是,当查询搜索确切的词时,如果没有匹配则不会显示任何结果,而我希望查询搜索的地方,比如用户输入“musec”,查询应该匹配“音乐”。意味着查询应该只在单词中寻找近似匹配。
任何人都可以告诉我如何完成此操作,在此先感谢。
这是我在基于 codeigniter 的所有代码项目中使用的 solution/code:
public function get_search_count($q)
{
$qarray = explode(' ', $q);
$this->db->distinct();
$this->db->select('*');
$this->db->from('products');
$this->db->like('product_name', $q, 'both');
if (count($qarray) > 1) {
foreach ($qarray as $s) {
$this->db->or_like('product_seo_title', $s, 'both');
$this->db->or_like('product_seo_description', $s, 'both');
$this->db->or_like('product_seo_keyword', $s, 'both');
}
} else {
$this->db->or_like('product_seo_title', $q, 'both');
$this->db->or_like('product_seo_description', $q, 'both');
$this->db->or_like('product_seo_keyword', $q, 'both');
}
$query = $this->db->get();
return $query->results();
}