mysql搜索。需要所有关键字

时间:2011-07-15 05:09:39

标签: mysql full-text-search

我有这个SQL查询:

SELECT DISTINCT url FROM paragraphs WHERE 
  MATCH (title) AGAINST('$query') ORDER BY 
  MATCH (title) AGAINST('$query') DESC

但是当我搜索“约翰史密斯”时,我会得到“约翰”,“约翰室”......等等。我怎样才能说出这两个字?

2 个答案:

答案 0 :(得分:0)

您可以在过滤掉所有不需要的匹配项的位置添加额外的LIKE子句:

SELECT DISTINCT url FROM paragraphs WHERE 
  MATCH (title) AGAINST('$query') AND title LIKE "%$query%"
  ORDER BY MATCH (title) AGAINST('$query') DESC

因此,您有得分,只有结果完全匹配。

答案 1 :(得分:0)

在布尔搜索中使用+应该包含所有单词:

<?php

$query = str_replace(" "," +",$query);

$sql = "SELECT DISTINCT url FROM paragraphs WHERE 
          MATCH (title) AGAINST('$query' IN BOOLEAN MODE))
          ORDER BY score DESC;"

?>

Dev.MySQL.com: 11.9.2. Boolean Full-Text Searches