1

我有一个简单的 mysql 搜索功能,可以搜索我想扩展的条目标题,以便能够搜索部分单词......

例如,假设我搜索:取消

目前它将返回标题中带有取消单词的任何内容。但理想情况下,它还应该返回标题中带有取消字样的任何内容。

这是我到目前为止所拥有的...

function search_title_body($q){
    $q = '+' . $q;
    $q = str_replace(' ', '+', $q);
    $q = "+(".$q.")";

    $query = "SELECT *,
              MATCH(title) AGAINST('$q' IN BOOLEAN MODE) AS score
              FROM results
              WHERE (MATCH(title) AGAINST('$q' IN BOOLEAN MODE))
              AND (published = 1)
              ORDER BY score DESC";

    $query = $this->db->query($query);

    $results = array();


    foreach ($query->result() as $row){
        $results[] = $row;
    }

    return $results;
} 

谁能指出我正确的方向?非常感谢您的帮助 :)

4

3 回答 3

0

关于什么

SELECT *,
MATCH(title) AGAINST('$q' IN BOOLEAN MODE) AS score
FROM results
WHERE title LIKE '%$q%'
AND published = 1
ORDER BY score DESC
于 2012-09-11T17:48:58.587 回答
0

我认为您正在寻找的是 LIKE 或 ILIKE 功能

$q = "somestring";
 $query = "SELECT *,              
          FROM results
          WHERE title ILIKE '%$q%'
          AND (published = 1)
          ORDER BY score DESC";

还有为什么要创建一个匹配列,num_rows 会给你然后点击数。

于 2012-09-11T17:49:32.010 回答
0

您可以使用 like 语句构建您的 $q 以获得这样的查询

SELECT * FROM results
WHERE title like '%cancel%' OR title like '%otherword%'
AND (published = 1)
ORDER BY score DESC
于 2012-09-11T17:49:53.117 回答