Solved

Ignoring white space on MySQL LIKE statement

Posted on 2007-03-21
6
437 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
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 142

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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

914 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now