tags:

views:

53

answers:

2

hi,

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.

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

or

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

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

Amarghosh
hi well just to make it much clearer the notes column contains text something like this"abc updated stage on project 13084 (Activity - 309)"now i need to pull 309 out and update avtivityId coulmn within the same table.
bharath
@Naktibalda: the same problem using LIKE.
bharath
@Bharath You cannot read from a table in the same query as you're updating it, if that's what you're trying to do.
Amarghosh
no i am not trying to do that i am trying to do something like this"UPDATE table_name SET activityId = {$f['activityId']} WHERE notes REGEXP '{$f['activityId']}' "now consider notes to be "abc updated stage on project 13084 (Activity - 309)".applying the above query assuming $f['activityId'] to be 30 the query will work and update activityId column with the value 30. which is exactly what my problem is. i need activityId column to be updated only if notes contained (Activity - 309) and $f['activityId'] = 309 as well. i am very sorry if i am not framing the question right.
bharath
@bharath If the `(activityid - ` part is guaranteed to be the same, why not use `like` as suggested in the other answer?
Amarghosh
yeah true but what if the notes had (activityid - 309) and $f['activityId'] = 30. LIKE will still match it won't it?i want it match only if notes had (activityid - 309) and $f['activityId'] = 309.
bharath
@bharath did you try my examples? because both of them takes that into account - what did you get?
Amarghosh
hi there you guys were right it was my fault....i've been doin a very silly mistake all along....sorry about that....and thanks a lot for your time, you've been a great help :)
bharath
+1  A: 

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']})%'
Naktibalda
your right as well....sorry about not mentioning earlier..thanks very much.
bharath