mysql全文搜索无法正常工作

时间:2014-08-29 04:48:19

标签: php mysql full-text-search

我正在尝试使用表中的MySQL全文搜索

查询:

$text="MySQL HTML";
$sql="SELECT * FROM search
          WHERE MATCH (title,title_info) AGAINST ('$text' IN BOOLEAN MODE)";

它应该返回所有包含MySQL HTML或MySQL或HTML的行, 但它没有产生任何原因?

3 个答案:

答案 0 :(得分:2)

您必须在每个搜索字词中添加+符号作为前缀:

$text="MySQL HTML";
$text_array = split(' ',$text);
$search_terms = '+'.join($text_array,' +');  // +MySQL +HTML
$sql="SELECT * FROM search
      WHERE MATCH (title,title_info) AGAINST ('$search_terms' IN BOOLEAN MODE)";

了解有关全文搜索的更多信息: http://dev.mysql.com/doc/refman/5.5/en/fulltext-boolean.html

答案 1 :(得分:-1)

试试这个

$text="MySQL HTML";
$sql=" SELECT * FROM search 
       WHERE title LIKE '%$text%' OR
       title_info LIKE '%$text%'";

答案 2 :(得分:-1)

尝试实现此功能将搜索全文

$text = "Test String For You";
$bquery .= getwQuery($text,'title,title_info');

$sql="SELECT * FROM search WHERE ".$bquery;
echo $sql; 
function getwQuery($data, $field_name)
{
    $names_exploded = explode(" ", $data);
    $fname = explode(",", $field_name);
    foreach($names_exploded as $each_name)
    {
        if($each_name != ""):
            $bquery .= " AND (";
            foreach($fname as $fn)
            {
                $bquery .= $fn." LIKE '%".$each_name."%' OR ";
            }
            $bquery = rtrim($bquery,' OR ');
            $bquery .= ')';
        endif;
    }
    $bquery = ltrim($bquery,' AND ');
    return $bquery;
}

<强>输出:
SELECT * FROM search WHERE(title LIKE'%Test%'或title_info LIKE'%Test%')AND(title LIKE'%String%'或title_info LIKE'%String%')AND(title LIKE'%For%'OR title_info LIKE'%For%')AND(标题LIKE'%You%'或title_info LIKE'%You%')