PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.databases.mysql > Query to return record only if search text is found as whole word
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Query to return record only if search text is found as whole word

Réponse
 
LinkBack Outils de la discussion
Vieux 21/10/2007, 12h58   #1
damezumari
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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

  Réponse avec citation
Vieux 21/10/2007, 16h29   #2
Brian Wakem
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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
  Réponse avec citation
Vieux 21/10/2007, 16h44   #3
Kees Nuyt
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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,%'
s 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)
  Réponse avec citation
Vieux 21/10/2007, 17h13   #4
Paul Lautman
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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,%'
> s 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)


  Réponse avec citation
Vieux 21/10/2007, 18h55   #5
Kees Nuyt
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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,%'
>> s 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)
  Réponse avec citation
Vieux 21/10/2007, 20h08   #6
Paul Lautman
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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,%'
>>> s 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


  Réponse avec citation
Vieux 22/10/2007, 16h34   #7
damezumari
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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

  Réponse avec citation
Vieux 22/10/2007, 16h57   #8
Captain Paralytic
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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?

  Réponse avec citation
Vieux 01/11/2007, 19h31   #9
Peter H. Coffin
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 01h25.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,14734 seconds with 17 queries