Jump to content

Recommended Posts

I built a simple search for a client which is working beautifully so far except for one issue I can seem to solve and I've been having trouble these last two weeks hunting for a solution.

 

I've been using MATCH() AGAINST() to perform simple boolean searchs when I have multiple terms and thats fine, and using LIKE when I'm only searching a single word.  The problem with each method is it keeps finding words within words.

 

For example when they search "tea" it brings up words like "team", "instead" and "steady".  What my client wants is for it to only bring up whole words.  Is there any query keywords or formats I can use to ensure this?

 

Thank you.

Link to comment
https://forums.phpfreaks.com/topic/161730-solved-query-problem/
Share on other sites

Sorry I should have explained that better.

 

I mean that if they search for the word "tea" I want it to only find the word "tea" and not words that happen to have the letters "t", "e" and "a" in them like "team", "instead" and "steady".

 

Is that clearer?

 

maybe im wrong in comprehending ur question. Based on ur question, y not just do like this..

 

SELECT * FROM TABLE WHERE COLUMN = 'TEA'

Link to comment
https://forums.phpfreaks.com/topic/161730-solved-query-problem/#findComment-853597
Share on other sites

Um, no.... you'll need LIKE '%TEA%'

 

Sorry.

 

My version is: MySQL 5.0.67

 

Here is my mysql query:

 

SELECT `id`, `content` FROM `mapContent` WHERE LOWER(`content`) LIKE '%tea%'

 

What is happening is that its grabbing words that happen to have "tea" inside of them such as "team", "instead" and "steady".

 

What my client wants is for it to only grab the word "tea".  For example if the record had.

 

"I like to drink tea in the morning"  <= it would grab this

 

But if it said:

 

"I want to drink coffee instead" <= it wouldn't grab this despite there being the letters "tea" in part of the text.

 

Is that descriptive enough?  I've tried using the like statement and I've also tried this:

 

 SELECT `id`, `content` FROM `mapContent` WHERE MATCH(`content`) AGAINST('+tea' IN BOOLEAN MODE)

 

And this:

 

 SELECT `id`, `content` FROM `mapContent` WHERE MATCH(`content`) AGAINST('+"tea"' IN BOOLEAN MODE)

 

I'm not getting any error messages, but i'm still not getting the desired result.

Link to comment
https://forums.phpfreaks.com/topic/161730-solved-query-problem/#findComment-853892
Share on other sites

Did you try regexp like variant?

 

select * from mapcontent where lower(content) rlike '[[:<:]]tea[[:>:]]'

 

(I am not expert in mysql so I am not sure how its regexp support works, this is just a hint  :) )

 

Wow, I wasn't aware you could do regular expression in MySQL.  Interesting timing since I only recently started teaching myself how to use them in PHP and AS3.  I'll try that.

 

Thank you.

Link to comment
https://forums.phpfreaks.com/topic/161730-solved-query-problem/#findComment-854083
Share on other sites

Try:

SELECT id, content
FROM mapContent
HAVING LOCATE(' tea ', content) > 0
     OR LOCATE('tea ', content) > 0
     OR LOCATE(' tea', content) > 0;

 

The space on tea should separate it from words like instead. Though there could be a punctuation mark at the end of tea. Guess you'll have to think about that. :D

Link to comment
https://forums.phpfreaks.com/topic/161730-solved-query-problem/#findComment-854216
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.