88

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

2
  • 2
    Caveat: This Question is about two words occurring in a particular order. Most of the Answers do not allow for matching "2100 Stylus". Commented Jan 27, 2019 at 23:08
  • If one of two ( or many) options, try select * from table1 where column1 REGEXP 'something|otherthing'; Commented Sep 21, 2022 at 6:02

7 Answers 7

133

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%'
Sign up to request clarification or add additional context in comments.

11 Comments

Thanks, this way i won't get 'HP Laserjet 2100'. Sorry if my example was not the best.
One of the repliers to the answer here compared LIKE / REGEX performance and found that LIKE performed better, FYI: stackoverflow.com/questions/1127088/mysql-like-in
@Amalgovinus thanks for the link, I wouldn't have thought so.. It's good to know :)
What if I have 3 words ?
@Florin in the first query .+ indicate any character (1 or more) between Stylus and 2100 if you want to add EPSON it would be EPSON.+'Stylus.+2100 in the second query you just need to add a new AND in the WHERE clause
|
31

I think that the best solution would be to use Regular expressions. It's cleanest and probably the most effective. Regular Expressions are supported in all commonly used DB engines.

In MySql there is RLIKE operator so your query would be something like:
SELECT * FROM buckets WHERE bucketname RLIKE 'Stylus|2100'
I'm not very strong in regexp so I hope the expression is ok.

Edit
The RegExp should rather be:

SELECT * FROM buckets WHERE bucketname RLIKE '(?=.*Stylus)(?=.*2100)'

More on MySql regexp support:
http://dev.mysql.com/doc/refman/5.1/en/regexp.html#operator_regexp

6 Comments

This regular expression does something different: it gets anything that contains either 'Stylus' or '2100', not necessarily both.
I've improved my RegExp
I was searching for a regexp only solution and this trick did it ^^,
By adding a | between the 2 () like this RLIKE '(?=.*Stylus)|(?=.*2100)' will give separate results -->> rows with Stylus only, rows with 2100 only OR rows with both... but not necessarily the 2 in the same row...
Did you try this? Never heard that MySql regex supports lookarounds.
|
18

You can just replace each space with %

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

4 Comments

haha! okay, that requirement was not specified on the question, "How can I select the record by searching 'Stylus 2100' ?"
I know this question is old, but is this not the most efficient solution? From what I can tell, this will match Photo Stylus 2100. It will match anything followed by Stylus followed by anything followed by 2100 followed by anything. If someone disagrees, please tell me why because it means I've fundamentally misunderstood something about like expressions.
@RTF I'm a bit late, but % is like + in Regex, not *. It matches something, not anything, so won't match Photo Stylus 2100 but would match Photo Stylus 2100x.
@tjbp - Wrong. LIKE's % matches the empty string. So it is essentially identical to RLIKE's .*. Melvin's answer stands; Alessio first comment is wrong. It is trivial to test: SELECT 'Photo Stylus 2100' LIKE '%Stylus%2100%'; returns 1 (meaning true).
8

The correct solution is a FullText Search (if you can use it) https://dev.mysql.com/doc/refman/5.1/en/fulltext-search.html

This nearly does what you want:

SELECT * FROM buckets WHERE bucketname RLIKE '(Stylus|2100)+.*(Stylus|2100)+';

SELECT * FROM buckets WHERE bucketname RLIKE '(Stylus|2100|photo)+.*(Stylus|2100|photo)+.*(Stylus|2100|photo)+.*';

But this will also match "210021002100" which is not great.

Comments

1

you need to do something like this,

SELECT * FROM buckets WHERE bucketname RLIKE 'Stylus.*2100';

or

SELECT * FROM buckets WHERE bucketname RLIKE '(Stylus)+.*(2100)+';

1 Comment

The second one gives allows the silly StylusStylus 210021002100 -- Why have the +??
0

Assuming that your search is stylus photo 2100. Try the following example is using RLIKE.

SELECT * FROM `buckets` WHERE `bucketname` RLIKE REPLACE('stylus photo 2100', ' ', '+.*');

EDIT

Another way is to use FULLTEXT index on bucketname and MATCH ... AGAINST syntax in your SELECT statement. So to re-write the above example...

SELECT * FROM `buckets` WHERE MATCH(`bucketname`) AGAINST (REPLACE('stylus photo 2100', ' ', ','));

Comments

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.