开发者

How can I restrict the SELECT MATCH AGAINST to return ony results that contains all searched terms?

开发者 https://www.devze.com 2023-03-22 10:05 出处:网络
If I my term is for example iphone ipod the following query SELECT id FROM products WHERE MATCH (name) AGAINST (\'$term\')

If I my term is for example iphone ipod the following query

SELECT id FROM products WHERE MATCH (name) AGAINST ('$term')

will give me results that contai开发者_如何转开发n both terms or only one of them.

How can I restrict to show me only results that contain both terms? Speed is my concern.

Thank you.


Add a + before every word:

$words = explode(" ", $term);
$all = implode(" +",$words);

$sql = 
   "SELECT id 
    FROM products
    WHERE MATCH (name) AGAINST ('+" . $all . "' IN BOOLEAN MODE)"

Documentation

Be sure to escape the input terms to prevent SQL Injection.

0

精彩评论

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