开发者

mysql search. Require all keywords

I have this sql query:

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

What happens though is when I search, for example, for "john smith" I get "john" "john chambers" ...etc. How can I make both words requi开发者_Go百科red?


Using + in a Boolean search should make all words required:

<?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


You could add an additional LIKE clause to your where to filter out all unwanted matches:

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

Thus you have scoring and only the results matching exactly.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜