Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL - Search a field ignoring spaces

What would be a good way to search a field, ignoring spaces in the search term AND the field?

Example:

SELECT * 
FROM tablename
WHERE NOSPACES(fieldname) LIKE '%{search term with spaces removed}%'

Real-world example:

My client has a website selling long block engines. However, they often get people searching on their site for "cat 3306 longblock" (instead of "cat 3306 long block"). We need the "long block" engines to show up when "longblock" is searched.

Note: it probably doesn't matter for purposes of this question, but this website is built using PHP 5.3 and phpActiverecord.

like image 881
Jamon Holmgren Avatar asked Mar 19 '13 16:03

Jamon Holmgren


People also ask

How do I ignore a space in SQL query?

SQL Server TRIM() Function The TRIM() function removes the space character OR other specified characters from the start or end of a string. By default, the TRIM() function removes leading and trailing spaces from a string. Note: Also look at the LTRIM() and RTRIM() functions.

How do I trim a space in mysql?

The TRIM() function returns a string that has unwanted characters removed. Note that to remove the leading spaces from a string, you use the LTRIM() function. And to remove trailing spaces from a string, you use the RTRIM() function.


3 Answers

You may also want to consider Sphinx or Lucene for full text like searches over mysql's fulltext. I have not used them personally but I hear both are superior to mysql's built in fulltext indexes.

One thing to note with all of these methods is that they require a special index to keep updated which adds more maintenance and server overhead.

Good article on sphinx: http://astellar.com/2011/12/replacing-mysql-full-text-search-with-sphinx/

For a no maintenance solution that does well for product name or numbers is just to split the user's search terms by spaces and add LIKE '%term%' for each to your query.

Additionally, I create a special search field that is the product name or number with its tokens alphabetized and spaces and symbols stripped. You need to update this field every time the product name changes. Then in addition to queries against the original field I OR search against this new stripped field. Example if you have a product name like 'cat 3306 longblock' then create a product_name_search field and place '3306catlongblock'. When a user searches for 3306 long-block You process the search by stripping symbols and splitting on spaces:

WHERE (other product name search clauses) OR 
    (product_name_search LIKE '%3306%' AND product_name_search LIKE '%longblock%')

This process is not as good as freetext but it does reasonably well for product names and numbers.

like image 198
Tim Santeford Avatar answered Nov 01 '22 14:11

Tim Santeford


I would suggest there are a number of approaches, depending on how simple you want to make it. I personally would use Full-Text Search using the principles set out here http://dev.mysql.com/doc/refman/5.0/en/fulltext-natural-language.html

But you could also strip the spaces out of the search term using a regular expression i.e.

$str=preg_replace('/\s+/', '', $str);
like image 37
diagonalbatman Avatar answered Nov 01 '22 14:11

diagonalbatman


There is a way using repace function. Example:

SELECT * FROM `products` where REPLACE(PartName, ' ', '') = REPLACE('0 1 3000 70 27', ' ', '')
like image 1
tomekK Avatar answered Nov 01 '22 14:11

tomekK