我有一个简单的mysql搜索功能,可以搜索我想要扩展的条目标题,以便能够搜索部分单词......
例如,我说我搜索:取消
目前它将返回标题中带有单词cancel的任何内容。但理想情况下,它还应该在标题中返回带有取消的字样。
这是我到目前为止所拥有的......
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;
}
有人能指出我正确的方向吗?非常感谢您的帮助:)
答案 0 :(得分:0)
怎么样?
SELECT *,
MATCH(title) AGAINST('$q' IN BOOLEAN MODE) AS score
FROM results
WHERE title LIKE '%$q%'
AND published = 1
ORDER BY score DESC
答案 1 :(得分:0)
我认为您正在寻找的是LIKE或ILIKE功能
$q = "somestring";
$query = "SELECT *,
FROM results
WHERE title ILIKE '%$q%'
AND (published = 1)
ORDER BY score DESC";
另外为什么要制作一个匹配列,num_rows会给你一些命中数。
答案 2 :(得分:0)
您可以使用like语句构建$ q以获取此类查询
SELECT * FROM results
WHERE title like '%cancel%' OR title like '%otherword%'
AND (published = 1)
ORDER BY score DESC