首页 > 解决方案 > 带模式的搜索字符串查询

问题描述

我有一张如下表

表格1

id type

1   Aches, Pain, Fever
2   Fever
3   Fever Blisters
4   Hay Fever
5   Typhoid Fever
6   Valley Fever
7   Viral Hemorrhagic Fever
8   Yellow Fever
9   Fever with chills
10  Fever well
11  Day fever
12  Fever from week
13  Dengue Fever
14  Fever with cold
15  Fever with chills One
16  Fever hi
17  high grade fever

表2

id type

1   Acute Valley Fever
2   Allergic Rhinitis (Hay Fever)
3   Breakbone Fever 
4   Coccidioidomycosis (Valley Fever)
5   Typhoid2 Fever
6   Valleyrt Fever

我正在尝试使用 php codeigniter

$this->db->select("id,type");
$this->db->from('table1');
$this->db->like('type', $searchterm, 'after');
$query1 = $this->db->get_compiled_select();

$this->db->select("id,type");
$this->db->from('table1');
$this->db->like('type', $searchterm, 'both');
$this->db->not_like('type', $searchterm, 'after');
$query2 = $this->db->get_compiled_select();

$searchResult = $this->db->query($query1 . " UNION " . $query2)->result();

if (sizeof($searchResult) > 0) {
    return $searchResult;
} else {
    return [];
}

我想使用关键字“发烧”在搜索中显示类型列表。我想要 table1 和 table2 的输出如下

  1. 首先显示以关键字“发烧”开头的类型
  2. 然后显示包含关键字 'fever' 作为子字符串的类型

我该怎么做才能从另一个表中获取记录?

标签: phpmysqlcodeigniter-3

解决方案


推荐阅读