Home > database >  MySQL 5.7.14 - how to match a LIKE against a column that sometime contains ' ' instea
MySQL 5.7.14 - how to match a LIKE against a column that sometime contains ' ' instea

Time:03-06

I have a MySQL statement with a LIKE. However, the column sometimes contains:

  

which then does not match. An example:

ad_detail LIKE 'Personal Growth - 1'

Column ad_detail contains:

Personal Growth - 1 //matches
Personal Growth - 1 //does not match
Personal Growth - 1 //does not match
Personal Growth - 1 //does not match

How can I fix this to match in all instances please.

CodePudding user response:

LIKE in MySQL has only two wildcards, one which matches any single character and one which matches any string of zero or more characters. These wildcards are _ and % respectively.

You could do this:

ad_detail LIKE 'Personal Growth%- 1'

But this would have the unintended behavior of matching a lot of other strings that you don't want it to.

Personal Growth - 1 //matches
Personal Growth - 1 //matches
Personal Growth - 1 //matches
Personal Growth - 1 //matches

Probably matches things you don't to match:

Personal Growth-1 //matches because % matches zero characters
Personal Growth is good for you -1 //matches because % matches any string of characters

You probably want to use a regular expression:

ad_detail REGEXP 'Personal Growth( | )-( | )1'

CodePudding user response:

I found this:

REPLACE(at_award_description.ad_detail, ' ', ' ') LIKE ?

Where ? is any string that I am matching against (e.g., 'Personal Growth - 1').

  • Related