Jump to content

regular expression in a query?


jeff5656

Recommended Posts

Could someone please help me with the code that will select records only if there are certain keywords in two of the fields?

 

for instance:

$query = "select * from table_a where (column_a REGEXP 'apple' OR 'kiwi') 
OR (column_b REGEXP 'apple' OR 'kiwi) WHERE status = 'active' ";

So if the record contains apple or kiwi anywhere in column_a OR column_b, then select that record.

I know you have to use regexp because the contents of column_a is a big text with a few sentences or paragraphs of text, and I only want to select the record if it contains those two words (and same for column_b).

 

 

Link to comment
Share on other sites

Not sure what's more expensive LIKE or REGEXP but I would assume REGEXP.  Why can't you use LIKE for each column?  Also, I don't think you need to separate your logic with parentheses if all your logical operators are ORs.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.