PHP mysql 多词搜索查询

标签 php mysql codeigniter

我进行了搜索查询(最多三个单词),但在增加单词数量时它不太有帮助,因此我需要一个用于 n 个代码的代码

这里是my site

 public function search() {
            $s_keywords = $this->input->get('q');           
            $arr = explode(" ", $s_keywords);

            if (count($arr) > 0) {
                $query = $this->db->query("SELECT 
                    vacancy_id, vc_title, vc_experience, vc_closedate, tbl_company_cmp_name 
                    FROM tbl_vacancy
                    WHERE CONCAT(vc_title, ' ', tbl_company_cmp_name)  =  '$s_keywords' ");             
                $result = $query->result();

                return array( 'job_search' => $result,);
            }

            if (count($arr) == 1) {
                $query = $this->db->query("SELECT vacancy_id, vc_title, vc_experience, vc_closedate, tbl_company_cmp_name FROM tbl_vacancy
                WHERE vc_title LIKE '%{$s_keywords}%' OR tbl_company_cmp_name LIKE '%{$s_keywords}%' ");
                $result = $query->result();

                return array( 'job_search' => $result,);
            } else if (count($arr) == 2) {
                $query = $this->db->query("SELECT vacancy_id, vc_title, vc_experience, vc_closedate, tbl_company_cmp_name FROM tbl_vacancy
                WHERE (vc_title LIKE '%{$arr[0]}%' OR tbl_company_cmp_name LIKE '%{$arr[0]}%' ) "
                . "AND (vc_title LIKE '%{$arr[1]}%' OR tbl_company_cmp_name LIKE '%{$arr[1]}%') ");
                $result = $query->result();

                return array( 'job_search' => $result,);
            } else if (count($arr) == 3) {
                $query = $this->db->query("SELECT vacancy_id, vc_title, vc_experience, vc_closedate, tbl_company_cmp_name FROM tbl_vacancy
                WHERE (vc_title LIKE '%{$arr[0]}%' OR tbl_company_cmp_name LIKE '%{$arr[0]}%' ) "
                . "AND (vc_title LIKE '%{$arr[1]}%' OR tbl_company_cmp_name LIKE '%{$arr[1]}%') "
                . "AND (vc_title LIKE '%{$arr[2]}%' OR tbl_company_cmp_name LIKE '%{$arr[2]}%') ");
                $result = $query->result();

                return array( 'job_search' => $result,);
            }
        }

最佳答案

尽你所能see here您需要一个 OR 语句(就像您所做的那样)或使用 REGEXP 进行查询。

无论哪种方式,您都需要创建查询字符串,然后运行查询。我会循环每个单词,例如

$query = 'SELECT vacancy_id, vc_title, vc_experience, vc_closedate, tbl_company_cmp_name FROM tbl_vacancy';
$wheres = array();

foreach ($arr as $keyword) {
$wheres[] = "vc_title LIKE '%{$skeyword}%' OR tbl_company_cmp_name LIKE '%{$keyword}%' ";
};

//join the where lines with 'AND'
$where = join(' AND ', $wheres);

$query = $query . ' WHERE ' . $where;
//run the query

$query = $this->db->query($query);
$result = $query->result();
return array( 'job_search' => $result,);

与构建正则表达式相同,在本例中我们不需要 foreach,因为数组中有单个单词:

$regex = join('|', $arr);
$query = 'SELECT vacancy_id, vc_title, vc_experience, vc_closedate, tbl_company_cmp_name FROM tbl_vacancy';

$where = " WHERE vc_title REGEXP '$regex' OR tbl_company_cmp_name  REGEXP '$regex'";
$query = $this->db->query($query . $where);
$result = $query->result();
return array( 'job_search' => $result,);

关于PHP mysql 多词搜索查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32033162/

相关文章:

php - 在 PHP 中编码 Amazon Flexible Payments secret 字符串的问题

php - 将数据从 MVC Controller 传递到 PHP 中的 View

php - 使用 php exec 函数执行 Node 命令时出错

php - SQL注入(inject): is this code safe?

jquery - 如何在 Tempus Dominus bootstrap 4 datetimepicker 中设置最小日期

mysql - 全文索引无法正常工作

mysql - MAX(总和(票数))|只获得赢家

mysql - SELECT COUNT 与 ORDER BY 混淆

php - 由于更改而更新数据库中的现有记录?

php - 如何在 CodeIgniter 分页中将类名设置为 <a> 标签