Solved

Ignoring white space on MySQL LIKE statement

Posted on 2007-03-21
6
451 Views
Last Modified: 2013-12-13
HI guys,

I have a search page feature on my website (PHP & MySQL 5). At the moment i am using SELECT * FROM table WHERE somefield LIKE '%keywords%'

The problem is the search became really sensitive when it comes to whitespace. Say if i want to search for "Radio A", i have to put "Radio A" for the search to find the record.  When someone put "RadioA" (Without spaces) it wont find any results.

I dont want to explode "Radio A" into "Radio" and "A", as it will return any other "Radio" results which we dont want.

Any hints and tips would be appreciated.

0
Comment
Question by:JaZziD
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 20

Expert Comment

by:steelseth12
ID: 18763230
SELECT * FROM table WHERE somefield LIKE '%keywords%' OR somefield LIKE '%trim(keywords)%'
0
 
LVL 48

Expert Comment

by:hernst42
ID: 18763290
regular expresion might help in thiscase (if your mysql-version supports it)
http://dev.mysql.com/doc/refman/5.0/en/regexp.html

in your programming langnguage use a function to replace any whitespace with an emoty string, then put between each char a \s*
so if the user enters "radioA"or "radio A" $keywords will be "r\s*a\*sd\s*i\s*o\s*A"
and the sql:

SELECT * FROM table WHERE somefield REGEXP 'r\s*a\*sd\s*i\s*o\s*A'
0
 
LVL 48

Accepted Solution

by:
hernst42 earned 250 total points
ID: 18763298
Yup error in the example. you need to replace any occurency of \s with [[:space:]] in my example
0
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 18763395
or, replace the whitespace by % before submitting the keyword for the search...
0
 

Author Comment

by:JaZziD
ID: 18770091
hi guys,

let me try what hernst suggested first
0
 

Author Comment

by:JaZziD
ID: 18770555
hi hernst... thanks for that, it works great.

angelll: when i tried to replace the whitespace with % it didnt work somehow
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
This article discusses how to create an extensible mechanism for linked drop downs.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question