1

I have a column in a table that contains the path for a file, along the lines of:

/here/here2/something.jpg

I need to change every row to map it to:

/here3/something.jpg

Is there an easy way to do this? Thanks!

0

2 Answers 2

1

You don't need regex for this. Simple string functions can do the trick. You could use a query like this:

update test set path=concat('/here3/',
    substring(path, length('/here/here2/') + 1))
    where path like '/here/here2/%';

Here is a little test case to prove it works:

mysql> create table test (path varchar(64));
Query OK, 0 rows affected (0.02 sec)

mysql> insert into test (path) values
    -> ('/here/here2/something.jpg'),
    -> ('/here/here2/something-else.jpg'),
    -> ('/here/here2/yet-another-something.jpg');
Query OK, 3 rows affected (0.01 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> select * from test;
+---------------------------------------+
| path                                  |
+---------------------------------------+
| /here/here2/something.jpg             |
| /here/here2/something-else.jpg        |
| /here/here2/yet-another-something.jpg |
+---------------------------------------+
3 rows in set (0.00 sec)

mysql> update test set path=concat('/here3/',
    -> substring(path, length('/here/here2/') + 1))
    -> where path like '/here/here2/%';
Query OK, 3 rows affected (0.00 sec)
Rows matched: 3  Changed: 3  Warnings: 0

mysql> select * from test;
+----------------------------------+
| path                             |
+----------------------------------+
| /here3/something.jpg             |
| /here3/something-else.jpg        |
| /here3/yet-another-something.jpg |
+----------------------------------+
3 rows in set (0.00 sec)
Sign up to request clarification or add additional context in comments.

Comments

0

First of all, are you familiar with using PHP? You could write a script to get all of the entries from the database, and then change them using preg_replace(). Then you could use mysql to update the database.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.