How to make sure that when querying Petya and Vasya in mySQL searched for a bunch of words and separately, as now only “Petya and Vasya” is looking for it alone, Petya or Vasya does not find it.

 select * from name WHERE NAME LIKE $name 

    2 answers 2

    There was such a question.

    In general, specifically for your situation, it turns out like this

     $words = explode(" ", $search_query); foreach ($words as $word){ $word = trim($word); if (strlen($word) > 3) //Слово не менее 3 символов $sql[] = 'NAME LIKE "%'.$word.'%"'; } $sql = 'SELECT * FROM name WHERE LIKE %'.$search_query.'% OR '.implode(" OR ", $sql); 

    Or, again, if RLIKE (but without checking for word length).

     $search = str_replace(" ","|", $search_query); $sql = 'SELECT * FROM clients WHERE name RLIKE "'.$search_query.'|'.$search.'" OR sname RLIKE "'.$search.'"'; 
       select * from name WHERE NAME LIKE %$name%