Query to return record only if search text is found as whole word

This is a discussion on Query to return record only if search text is found as whole word within the MySQL Database forums, part of the Database Forums category; With the where condition like '%hell%' records with 'hello', 'shell', etc will be returned in addition to records where 'hell' ...


Go Back   Usenet Forums > Database Forums > MySQL Database

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 10-21-2007
damezumari
 
Posts: n/a
Default Query to return record only if search text is found as whole word

With the where condition

like '%hell%'

records with 'hello', 'shell', etc will be returned in addition to
records where 'hell' is found as a word by itself.

How do I write a condition that will ONLY return records where 'hell'
is found as a word by itself?

Regards,

Jan Nordgreen

Reply With Quote
  #2 (permalink)  
Old 10-21-2007
Brian Wakem
 
Posts: n/a
Default Re: Query to return record only if search text is found as whole word

damezumari wrote:

> With the where condition
>
> like '%hell%'
>
> records with 'hello', 'shell', etc will be returned in addition to
> records where 'hell' is found as a word by itself.
>
> How do I write a condition that will ONLY return records where 'hell'
> is found as a word by itself?
>



Either use a FULLTEXT index or a regex that looks for word boundaries (very
slow).


--
Brian Wakem
Reply With Quote
  #3 (permalink)  
Old 10-21-2007
Kees Nuyt
 
Posts: n/a
Default Re: Query to return record only if search text is found as whole word

On Sun, 21 Oct 2007 03:58:01 -0700, damezumari
<jannordgreen@gmail.com> wrote:

>With the where condition
>
>like '%hell%'
>
>records with 'hello', 'shell', etc will be returned in addition to
>records where 'hell' is found as a word by itself.
>
>How do I write a condition that will ONLY return records where 'hell'
>is found as a word by itself?


LIKE '% hell %'

Ok, this misses occurences with punctation instead of
spaces.

LIKE '% hell %' OR LIKE '% hell.%' OR LIKE '% hell,%'
helps a lot, but might not be good enough for your
purpose. Then it's time to go try FULL TEXT indexes, like
Brian suggests.

>Regards,
>
>Jan Nordgreen

--
( Kees
)
c[_] I don't want to be your other half.
I believe that One and One make TWO.
(Alanis Morrisette: "Not the Doctor") (#185)
Reply With Quote
  #4 (permalink)  
Old 10-21-2007
Paul Lautman
 
Posts: n/a
Default Re: Query to return record only if search text is found as whole word

Kees Nuyt wrote:
> On Sun, 21 Oct 2007 03:58:01 -0700, damezumari
> <jannordgreen@gmail.com> wrote:
>
>> With the where condition
>>
>> like '%hell%'
>>
>> records with 'hello', 'shell', etc will be returned in addition to
>> records where 'hell' is found as a word by itself.
>>
>> How do I write a condition that will ONLY return records where
>> 'hell' is found as a word by itself?

>
> LIKE '% hell %'
>
> Ok, this misses occurences with punctation instead of
> spaces.
>
> LIKE '% hell %' OR LIKE '% hell.%' OR LIKE '% hell,%'
> helps a lot, but might not be good enough for your
> purpose. Then it's time to go try FULL TEXT indexes, like
> Brian suggests.

Of course, they miss hell at the beginning or end of a field and so won't
find:
"Hello I must be going" (as said by Groucho Marx)


Reply With Quote
  #5 (permalink)  
Old 10-21-2007
Kees Nuyt
 
Posts: n/a
Default Re: Query to return record only if search text is found as whole word

On Sun, 21 Oct 2007 16:13:18 +0100, "Paul Lautman"
<paul.lautman@btinternet.com> wrote:

>Kees Nuyt wrote:
>> On Sun, 21 Oct 2007 03:58:01 -0700, damezumari
>> <jannordgreen@gmail.com> wrote:
>>
>>> With the where condition
>>>
>>> like '%hell%'
>>>
>>> records with 'hello', 'shell', etc will be returned in addition to
>>> records where 'hell' is found as a word by itself.
>>>
>>> How do I write a condition that will ONLY return records where
>>> 'hell' is found as a word by itself?

>>
>> LIKE '% hell %'
>>
>> Ok, this misses occurences with punctation instead of
>> spaces.
>>
>> LIKE '% hell %' OR LIKE '% hell.%' OR LIKE '% hell,%'
>> helps a lot, but might not be good enough for your
>> purpose. Then it's time to go try FULL TEXT indexes, like
>> Brian suggests.

>Of course, they miss hell at the beginning or end of a field and so won't
>find:
>"Hello I must be going" (as said by Groucho Marx)


You're right, so we'll make it:

x LIKE 'hell %' OR x LIKE '% hell' OR
x LIKE '% hell %' OR x LIKE '% hell.%' OR
x LIKE '% hell,%'

FULL TEXT indexes are getting more attractive ;)
--
( Kees
)
c[_] ...universities truly are storehouses of knowledge: students arrive
from school confident that they know very nearly everything, and
they leave years later certain that they know practically nothing.
Where did all the knowledge go in the meantime? Into the
university, of course, where it is carefully dried and stored.
(Terry Pratchett, Ian Stewart and Jack Cohen) (#97)
Reply With Quote
  #6 (permalink)  
Old 10-21-2007
Paul Lautman
 
Posts: n/a
Default Re: Query to return record only if search text is found as whole word

Kees Nuyt wrote:
> On Sun, 21 Oct 2007 16:13:18 +0100, "Paul Lautman"
> <paul.lautman@btinternet.com> wrote:
>
>> Kees Nuyt wrote:
>>> On Sun, 21 Oct 2007 03:58:01 -0700, damezumari
>>> <jannordgreen@gmail.com> wrote:
>>>
>>>> With the where condition
>>>>
>>>> like '%hell%'
>>>>
>>>> records with 'hello', 'shell', etc will be returned in addition to
>>>> records where 'hell' is found as a word by itself.
>>>>
>>>> How do I write a condition that will ONLY return records where
>>>> 'hell' is found as a word by itself?
>>>
>>> LIKE '% hell %'
>>>
>>> Ok, this misses occurences with punctation instead of
>>> spaces.
>>>
>>> LIKE '% hell %' OR LIKE '% hell.%' OR LIKE '% hell,%'
>>> helps a lot, but might not be good enough for your
>>> purpose. Then it's time to go try FULL TEXT indexes, like
>>> Brian suggests.

>> Of course, they miss hell at the beginning or end of a field and so
>> won't find:
>> "Hello I must be going" (as said by Groucho Marx)

>
> You're right, so we'll make it:
>
> x LIKE 'hell %' OR x LIKE '% hell' OR
> x LIKE '% hell %' OR x LIKE '% hell.%' OR
> x LIKE '% hell,%'
>
> FULL TEXT indexes are getting more attractive ;)


ahh, but that means that you will get "shell" and "hello", when the OP
wanted only things like:
"Hell is very cold tonight.

Bring on the FULLTEXT


Reply With Quote
  #7 (permalink)  
Old 10-22-2007
damezumari
 
Posts: n/a
Default Re: Query to return record only if search text is found as whole word

Thanks for enlightening me on fulltext indexing! and why there is no
good alternative. :)

Regards,

Jan Nordgreen

Reply With Quote
  #8 (permalink)  
Old 10-22-2007
Captain Paralytic
 
Posts: n/a
Default Re: Query to return record only if search text is found as whole word

On 22 Oct, 15:34, damezumari <jannordgr...@gmail.com> wrote:
> Thanks for enlightening me on fulltext indexing! and why there is no
> good alternative. :)
>
> Regards,
>
> Jan Nordgreen


What a strange question. If you build a FULLTEXT function to do a
particular job, why would you bother to build a good alternative?

Reply With Quote
  #9 (permalink)  
Old 11-01-2007
Peter H. Coffin
 
Posts: n/a
Default Re: Query to return record only if search text is found as whole word

On Sun, 21 Oct 2007 03:58:01 -0700, damezumari wrote:
> With the where condition
>
> like '%hell%'
>
> records with 'hello', 'shell', etc will be returned in addition to
> records where 'hell' is found as a word by itself.
>
> How do I write a condition that will ONLY return records where 'hell'
> is found as a word by itself?


WHERE my_column REGEXP '[[:<:]]hell[[:>:]]';

http://dev.mysql.com/doc/refman/5.0/en/regexp.html

--
17. When I employ people as advisors, I will occasionally listen to their
advice.
--Peter Anspach's list of things to do as an Evil Overlord
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 09:13 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0