开发者

mysql search. Require all keywords

开发者 https://www.devze.com 2023-03-20 06:36 出处:网络
I have this sql query: SELECT DISTINCT url FROM paragraphs WHERE MATCH (title) AGAINST(\'$query\') ORDER BY

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

精彩评论

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