Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL SELECT LIKE or REGEXP to match multiple words in one record

The field table.name contains 'Stylus Photo 2100' and with the following query

SELECT `name` FROM `table` WHERE `name` LIKE '%Stylus 2100%' 

I get no results. Of course i would if i searched

SELECT `name` FROM `table` WHERE `name` LIKE '%Photo 2100%' 

How can I select the record by searching 'Stylus 2100' ?

Thanks

like image 364
Alessio Firenze Avatar asked Feb 01 '12 16:02

Alessio Firenze


People also ask

Is regex faster than like?

Also LIKE is much faster than REGEXP.

What is the difference between like and REGEXP operators in MySQL?

Basically, LIKE does very simple wildcard matches, and REGEX is capable of very complicated wildcard matches. In fact, regular expressions ( REGEX ) are so capable that they are [1] a whole study in themselves [2] an easy way to introduce very subtle bugs.

What does REGEXP do in MySQL?

MySQL REGEXP performs a pattern match of a string expression against a pattern. The pattern is supplied as an argument. If the pattern finds a match in the expression, the function returns 1, else it returns 0. If either expression or pattern is NULL, the function returns NULL.


1 Answers

Well if you know the order of your words.. you can use:

SELECT `name` FROM `table` WHERE `name` REGEXP 'Stylus.+2100' 

Also you can use:

SELECT `name` FROM `table` WHERE `name` LIKE '%Stylus%' AND `name` LIKE '%2100%' 
like image 156
SERPRO Avatar answered Sep 19 '22 16:09

SERPRO