MySQL Query searching multiple keywords in a long string using php

Example keyword search "CAR WASH"
a car wash should appear in a line regardless of position

The search string can be very long, and the query must search for these keywords together and separately. There may be more than two keywords.

$query="SELECT * FROM TABLE WHERE zip LIKE'abc'"; $key= explode(" ", $keywords); for($i = 0; $i < count($key); $i++){ $query.=" AND (dealTitle LIKE '".$key[$i]." %' OR dealTitle LIKE '% ".$key[$i]." %' OR dealTitle LIKE '% ".$key[$i]." ')"; } 

However, the query returns those records containing β€œcar” or β€œwash”.

If I use the β€œcar wash”, both words should appear at least once. Any suggestion on how to change my search to accept this condition?

+4
source share
5 answers
 for($i = 0; $i < count($key); $i++){ $query.=" AND (dealTitle rlike '[[:<:]]".$key[$i]."[[:>:]]' )"; } 

Must use AND

Edition:

 for($i = 0; $i < count($key); $i++){ $query.=" AND (dealTitle like '% ".$key[$i]." %' or dealTitle like '% ".$key[$i]."' or dealTitle like '".$key[$i]."%') "; } 
+1
source

If you are using the mysql database, there is a full-text search function that will be a reliable and scalable solution.

 $query .= " AND MATCH(dealTitle) AGAINST('". $searchString ."')"; 

http://dev.mysql.com/doc/refman/5.0/en/fulltext-search.html#function_match

Check out this article to understand the FULLTEXT SEARCH function in MySQL.

+4
source

try the following:

  AND (CONCAT(' ',dealTitle,' ') LIKE '% car %' and CONCAT(' ',dealTitle,' ') LIKE '% wash %' ) 
0
source

Try this query: -

 AND (dealTitle LIKE '%car wash%' ) 
0
source

You are basically trying to achieve what the search engine does - full-text indexing. LIKE '%keyword%' is a partial match, as you already know. It not only processes a partial word, but also very slowly, because db has to read and compare each record on disk.

Consider using a full-text indexer. MySQL specifically supports it. See FULLTEXT

0
source

All Articles