0

Currently, my SQL search query use LIKE clause with %some-word-to-search% wildcards.

The problem of using percent wildcard is that it's returning all the rows which containing a part of my search param, e,g:

If I search car It will also show cartoons to me! is there any way to compare like clause only with entire words instead of part of it?

Any suggestion would be appreciated...

iSun
  • 1,714
  • 6
  • 28
  • 57

2 Answers2

3

You can use a regular expression that matches word boundary. For example, this will match rows where the text contains "car" as a single word, even when it's the first or last word, or adjacent to punctuation:

SELECT * FROM mytable WHERE mytext REGEXP '[[:<:]]car[[:>:]]';

Note that these kinds of queries require a table scan. A full text index can give better performance.

Joni
  • 108,737
  • 14
  • 143
  • 193
0

You can also look at the natural language search function match():

http://dev.mysql.com/doc/refman/5.0/en/fulltext-natural-language.html

Kevin Seifert
  • 3,494
  • 1
  • 18
  • 14