开发者

mysql LIKE = imprecise search

开发者 https://www.devze.com 2023-01-26 02:44 出处:网络
I\'m using a simple query for my search: SELECT * FROM table WHERE field LIKE \'%term%\' if I have a field = \"Company Name 123\" and I search for开发者_StackOverflow Company 123 the result is null

I'm using a simple query for my search:

SELECT * FROM table WHERE field LIKE '%term%'

if I have a field = "Company Name 123" and I search for开发者_StackOverflow Company 123 the result is null

how can I improve this? it only finds if the term is in sequence


Replace spaces with %

$newTerm = str_replace(' ', '%', $term);
$sql = "SELECT * FROM table WHERE field LIKE '%$term%'"

$r = mysql_qery($sql, $conn);


You need to put a % between Company and 123 in order for it to match. You might want to check out full text search functions.


try to replace spaces
$searchtext =str_replace(' ','%',$searchtext);


you could:

  • split your searchterm into words and build a query with a lot of ANDs (or ORs if you just want to find one of the parts) out of it (ugly, but i've seen this a lot of times)
  • replace ' '(space) with % (thats a wildcard) in your term (the way to go)
0

精彩评论

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