Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mysql update with regexp

Tags:

regex

mysql

I want to remove something from my table 1) 32) 121) 1000)... the format is number + )

I tried this code.

UPDATE articles SET 
title= REPLACE(title,'\d)', '' ) 
WHERE title regexp "\d)*"

Nothing happened in phpmyadmin, how to write correct? Thanks.

like image 471
cj333 Avatar asked May 05 '12 19:05

cj333


3 Answers

I have unique requirement where I need to replace inactive owner username. Where username contians INACITVE followed by village id. So I have used concat() funacion inside replace() function to replace dynamically.

 update Owner set username = replace(username, concat('_INACTIVE_',village_id) ,'') 
where village_id = 3363010;
like image 195
rajeev pani.. Avatar answered Sep 21 '22 19:09

rajeev pani..


You can't: Mysql doesn't support regex-based replace.

See this SO question for a work-around.

like image 8
Bohemian Avatar answered Nov 19 '22 04:11

Bohemian


Finally, I use some php to solve this problem with a quickly method.

for ($i=1; $i<=9999; $i++){
 $my_regex = $i.')';
 mysql_query("UPDATE articles SET title = REPLACE(title,'".$i."', '' ) where title like '%".$i."%'");
}
like image 2
cj333 Avatar answered Nov 19 '22 05:11

cj333