开发者

Mysql REGEXP how to do an exact match

i have a notes column which contains text and has an id within the text, something like "some random text (actvityid - 1234)"

i need to pull out the id 1234 in this case and update the activityid column within the same table.

my query looks like this

"UPDATE table_name SET activityId = {$f['activityId']} WHERE notes REGEXP '{$f['activityId']}' "
开发者_运维技巧

the problem with this is if $f['activityId'] is 34 or 123 for example it still updates the activityid column with that value. How can i do an exact match on "1234" and update only if it matches the whole string, here "1234".

Many thanks.


WHERE notes REGEXP CONCAT('(actvityid - ', {$f['activityId']}, ')')

or

WHERE notes REGEXP '[[:<:]]{$f['activityId']}[[:>:]]'

[[:<:]] and [[:>:]] stands for word boundaries.


No need to use CONCAT if variable is passed from PHP, and no need to use REGEXP if you match exact string without special characters

WHERE notes LIKE '%(actvityid - {$f['activityId']})%'
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜