1

I am trying to replace a string like "example.com" with "newdomain.com" in a MySQL table called "wp_options" with a column name "option_value".

I have seen a lot of other questions about replacement but the problem is that they are all using the REPLACE function, which damages the structure of my table because it removes the row (if matches) and inserts a new one(1), which makes the primary key and unique id disappears (for example, this is causing me to lose my theme configuration).

Is there a way to replace such string using the INSERT function? or the UPDATE?

1
  • 1
    Those solutions do not use the REPLACE() function. They are using the REPLACE statement. The two are quite different. Commented Oct 15, 2017 at 11:59

1 Answer 1

1

A simple update statement should do it:

UPDATE wp_options
SET    option_value = 'newdomain.com'
WHERE  option_value = 'example.com'

EDIT:
If the requirement is to search within the value as clarified by the comment below, you can use the replace function:

UPDATE wp_options
SET    option_value = REPLACE(option_value, 'example.com', 'newdomain.com')
WHERE  option_value LIKE '%example.com%'
Sign up to request clarification or add additional context in comments.

2 Comments

But this doesn't search for the string in all the column, it just replaces it if it equals example.com, what I am asking for is to search and replace as a string (I mean, even if i have something like example.com/posts/something, it should replace it here too).
@Madno I misunderstood the question then. See my edited answer for a solution that should accommodate this requirement.

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.