Arthur Frankel
Arthur Frankel

Reputation: 4705

Replace a column value in a table using regex in mysql

I need to do the following and I'm struggling with the syntax:

I have a table called 'mytable' and a column called 'mycolumn' (string).

In mycolumn the value is a constructed value - for example, one of the values is: 'first:10:second:18:third:31'. The values in mycolumn are all using the same pattern, just the ids/numbers are different.

I need to change the value of 18 (in this particular case) to a value from another tables key. The end result for this column value should be 'first:10:second:22:third:31' because I replaced 18 with 22. I got the 22 from another table using the 18 as a lookup value.

So ideally I would have the following:

UPDATE mytable 
SET mycolumn = [some regex function to find the number between 'second:' and ":third" - 
let's call that oldkey - and replace it with other id from another table - 
(select otherid from tableb where id = oldkey)].

I know the mysql has a REPLACE function but that doesn't get me far enough.

Upvotes: 2

Views: 3885

Answers (3)

Alex W
Alex W

Reputation: 38193

You want something like this, where it matches the group:

WHERE REGEXP 'second:([0-9]*):third'

However, MySQL doesn't have a regex replace function, so you would have to use a user-defined function:

REGEXP_REPLACE?(text, pattern, replace [,position [,occurence [,return_end [,mode]]])

User-defined function is available here:

http://www.mysqludf.org/lib_mysqludf_preg/

Upvotes: 1

AllInOne
AllInOne

Reputation: 1450

You can create your own function. I am scared of REGEX so I use SUBSTRING and SUBSTRING_INDEX.

CREATE FUNCTION SPLIT_STRING(str VARCHAR(255), delim VARCHAR(12), pos INT)
RETURNS VARCHAR(255)
RETURN REPLACE(SUBSTRING(SUBSTRING_INDEX(str, delim, pos),
   LENGTH(SUBSTRING_INDEX(str, delim, pos-1)) + 1),
   delim, '');

SPLIT_STRING('first:10:second:18:third:31', ':', 4)

returns 18

Based on this answer:

Equivalent of explode() to work with strings in MySQL

Upvotes: 1

Doug
Doug

Reputation: 869

The problem with MySQL is it's REGEX flavor is very limited and does not support back references or regex replace, which pretty much makes it impossible to replace the value like you want to with MySQL alone.

I know it means taking a speed hit, but you may want to consider selecting the row you want with by it's id or however you select it, modify the value with PHP or whatever language you have interfacing with MySQL and put it back in with an UPDATE query.

Generally speaking, REGEX in programming languages is much more powerful.

If you keep those queries slim and quick, you shouldn't take too big of a speed hit (probably negligible).

Also, here is documentation on what MySQL's REGEX CAN do. http://dev.mysql.com/doc/refman/5.1/en/regexp.html

Cheers

EDIT:

To be honest, eggyal's comment makes a whole lot more sense for your situation (simple int values). Just break them up into columns there's no reason to access them like that at all imo.

Upvotes: 1

Related Questions