1

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%

user187580
  • 2,275
  • 11
  • 32
  • 39

5 Answers5

2

Are you looking for longs strings containing the whole word?

Search for "whole word match" in MySQL

Community
  • 1
  • 1
Matt Ball
  • 354,903
  • 100
  • 647
  • 710
1

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
  • 377,238
  • 77
  • 533
  • 578
  • @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 Apr 20 '10 at 17:17
  • I dont think this is what the OP wants. *How do I search **anywhere in the description** for "house" only* – Adriaan Stander Apr 20 '10 at 17:17
  • 1
    @astander: yes his question is not that clear. Let's see what exactly is he looking for :) – Sarfraz Apr 20 '10 at 17:20
1

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
  • 12,556
  • 2
  • 34
  • 50
0

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.

Community
  • 1
  • 1
hookedonwinter
  • 12,436
  • 19
  • 61
  • 74
0

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
  • 3,221
  • 5
  • 28
  • 38