1

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?

3
  • your product_code column is varchar in type and you store values in comma separated form, Am I right? Commented May 6, 2016 at 9:43
  • Do you want to match exactly BO BB AF or only a part of it with product_code? Commented May 6, 2016 at 9:46
  • Only part of it. Matching any of the 2 character codes in the array value to the 2 character code in the DB column. The DB column will always contain only one 2 character code. Hope that makes sense? Commented May 6, 2016 at 9:50

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

Comments

2

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'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.