3

Here is my situation:

My company hosts multiple sites for clients, and the content of the sites are stored in a MySQL database.

I have an issue where I need to go through the whole database and replace pieces of the content. For example:

I need to replace http://downloads.mysever.com/siteID/someImage.jpg with //downloads.mysever.com/siteID/someImage.jpg

So I just need to remove the http: from all links that are going to our downloads server.

My question is what would be the best way to go about this? Will I need to write a php script to handle this, or am I able to use just MySQL to accomplish this.

Thanks

7
  • There is no "best way" to do something so trivial. Use whatever tools you are most comfortable with. Commented Dec 19, 2012 at 16:29
  • 1
    UPDATE table SET col = SUBSTR(col, 6) Commented Dec 19, 2012 at 16:31
  • So is this possible just using MySQL? I wasn't sure if regular expressions were possible in MySQL. I would prefer to write a query that would be able to handle it, but if needed I can write a PHP script. Commented Dec 19, 2012 at 16:31
  • @noub Why would you need a regex? You're just stripping the first 5 chars. At worst just find/replace http: with an empty string, still doesn't require a regex Commented Dec 19, 2012 at 16:32
  • 1
    Regular expressions are not a magic wand that is the best solution to every problem that relates to strings. Commented Dec 19, 2012 at 16:56

5 Answers 5

2

you could do this with pure mysql:

update table set field = REPLACE(field, 'http:', '') where INSTR(field, 'http:') > 0;

see this question

Sign up to request clarification or add additional context in comments.

1 Comment

Note that the WHERE condition is not required
2

you can use the sql statement

 UPDATE tablename SET url = REPLACE(url, 'http:', '');

Read this blog post

You can execute this command using the command line

mysql -uUSERNAME -p DATABASE_NAME -e "UPDATE tablename SET url = REPLACE(url, 'http:', '')";

Comments

0

If you're able to take the database offline briefly, I've found the easiest way is to:

  1. use mysqldump to export the entire database
  2. run a find-and-replace in whichever editor you prefer to update the database dump
  3. recreate the database from the dump file - make sure it all went correctly, then replace the original database with the new version.

Comments

0

MySQL does offer regular expression functionality. The functionality is probably too detailed to go into here so here is the link:

http://dev.mysql.com/doc/refman/5.5/en/regexp.html

A basic usage example for you sample case would be:

UPDATE table SET field = REPLACE(field, 'http:', '') WHERE field REGEXP '^http:'

Note however that this case could be done without regular expression by simply doing this:

UPDATE table SET field = REPLACE(field, 'http:', '') WHERE field LIKE 'http:%'

So you might not need to use regular expressions for each type of replacement you want to do.

Comments

0

To remove the leading 'http:' string (from a character type column) of all rows in a table, then something as simple as this will work:

UPDATE mytable
   SET mycol = SUBSTRING(mycol,6)
 WHERE mycol LIKE 'http:%'

UPDATE:

Note that using the REPLACE function will replace ALL occurrences of the specified string from a string; it may not be appropriate for removing just the leading occurrence of a specified string.

If you want to remove ALL occurrences of 'http:' found within column values, then:

UPDATE mytable
   SET mycol = REPLACE( mycol , 'http:, '')
 WHERE mycol LIKE '%http:%'

MySQL does provide support for regular expressions, specifically, for finding values that match a given regular expression; but MySQL does not provides support for "replacement" using a regular expression.

So you could use this in a query:

 WHERE mycol REGEXP 'http:'

in place of (equivalent to):

 WHERE mycol LIKE '%http:%'

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.