tags:

views:

46

answers:

5

Hello

keyword = house

SELECT * FROM products WHERE description LIKE '%house%'

This query also returns records having keyword, for example, courthouse, but I want to look for house only. How do I search anywhere in the description for "house" only?

Thanks.

UPDATE

just for more clarification ..

actually house, can be at 
  -  the start of the description .. "House for sale..", 
  -  can be quoted -- "house", 'house'
  -  can have exclamation .. house!!!, house!
  - others - house? etc .. 

this is why I used %house%

+1  A: 

The % before and after will search any matching text before and after the search keyword, try this instead for exact search:

SELECT * FROM products WHERE description = 'house'
Sarfraz
@astander: this is what he says: "This query also returns records having keyword, for example, courthouse, but I want to look for house only."
Sarfraz
I dont think this is what the OP wants. *How do I search **anywhere in the description** for "house" only*
astander
@astander: yes his question is not that clear. Let's see what exactly is he looking for :)
Sarfraz
I have updated question.
A: 

Try a space around the keyword:

SELECT * FROM products WHERE description LIKE '% house %' OR description LIKE 'house %' OR description LIKE '% house' OR description LIKE '% house%'

Edit: added more options in search. The last one would allow for house. or house,, though something like housecat would also match. Regex would work best here. This solution came from Astandar, who deleted his answer.

hookedonwinter
What if the keyword is at the end of a sentence? "I have an awesome house!"
Aaron W.
astander's solution is better than mine, as it allows the desc to start or end with the keyword too.
hookedonwinter
@Aaron ya, just realized that. Astander has the right answer.
hookedonwinter
Or had.. I'll fix mine
hookedonwinter
I think that the **REGEX** option from [@Bears will eat you] seemed like a better solution, that is why I removed my answer X-)
astander
Ya, once I thought about punctuation I realize that was better too. So, @user187580, [@Bears will eat you] has the best answer, imo, but if you don't want to play with regex, see mine or @Karthik
hookedonwinter
+2  A: 

Are you looking for longs strings containing the whole word?

http://stackoverflow.com/questions/656951/search-for-whole-word-match-in-sql

Matt Ball
I think this is the solution, I posted explicit one before checking yours
Unreason
I am looking at thelink. Also I have updated question. Thx
A: 

SELECT * FROM products WHERE description rlike '[[:<:]]house[[:>:]]'

rlike is synonim for REGEXP.

[[:<:]] denotes the start of the word and the
[[:>:]] end of the word.

It works for all your requirements (case insensitive, with quoted words or words ending, or begging, with exclamation points and other non-letter characters)

Unreason
can you please put some details so that I can modify accordingly, if required?
is it clear now?
Unreason
A: 

Simple thing only house means, use this

   SELECT * FROM products WHERE description = 'house'

If you want any word contain house in backside means use this,

   SELECT * FROM products WHERE description LIKE '%house'

If you want any word contain house in frontside means use this,

   SELECT * FROM products WHERE description LIKE 'house%'

If you want any word contain house in anywhere in description means use this,

   SELECT * FROM products WHERE description LIKE '%house%'
Karthik