Robin Bantjes
Robin Bantjes

Reputation: 299

MySQL regular expression matching

I have an array that contains some sting values that need to be matched in a similar fashion to or where. The array looks like this:

array('IS','KG','BO BB AF', 'MA')

When using a where clause I can match the 1st second and third values

SELECT * WHERE product_code LIKE 'IS'

but the 3rd value 'BO BB AF' needs to be matched like this

SELECT * WHERE product_code LIKE 'BO' or 'BB' or 'AF'

Is this possible to do with a single MySQL statement with a regular expression or will I have to break up the string and loop through it?

Upvotes: 1

Views: 266

Answers (2)

RomanPerekhrest
RomanPerekhrest

Reputation: 92904

Use REGEXP operator for such case:

SELECT * WHERE product_code REGEXP 'IS|KG|BO|BB|AF|MA';

http://dev.mysql.com/doc/refman/5.7/en/regexp.html

Upvotes: 2

Narendrasingh Sisodia
Narendrasingh Sisodia

Reputation: 21422

I'm not sure about the feasibility of this query but you can use something like as using REGEXP

SELECT * WHERE product_code REGEXP 'IS|KG|BO|BB|AF|MA'

Upvotes: 2

Related Questions