MySQL: Whole Word Search
Question by Compi | 08/06/2016 at 23:22
I would like to search my MySQL database for whole words. That means, when searching for the keyword "the", I only want to get the records in which there is really occurring "the" as a whole and individual word.
Up to now, my approach was to work with LIKE '%keyword%'. However, in the case quite as mentioned above, I also got hits for "them" or "nevertheless" what I do not want.
Because of that, I gave LIKE ' %keyword% ' a try. The spaces at the beginning and at the end made that this query really only found data records in which my keyword occurred as a whole word. However, it came to problems at another point: records beginning or ending with the keyword was not found anymore, because they do not have a blank character at their start and end.
So, I came to the query SELECT * FROM tab WHERE col LIKE '% keyword %' OR col LIKE 'keyword %' OR col LIKE 'keyword %' OR col = 'keyword'. This leads to quite better results, but there was another problem: it was not working with punctuation. As soon as there was a point or a comma after the keyword, it was not working anymore.
Now I'm at my wits end. Does someone has any idea how I can solve this and how to cover all of this case is? I think, adding all possible combinations with all possible punctuation, would be too much.
Tip | 1 Comment
Info | 0 Comments
Tip | 0 Comments
Info | 0 Comments
Question | 1 Answer
Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.
Ask your own question or write your own articles on askingbox.com. How to do.