6

I have a wordpress installation and I've broken the mysql database. For posts the urls are listed as '.../wordpress//...' instead of '.../wordpress/...'

How in SQL can I go through every row in the table and (perhaps use a regular expression) to replace every instance of 'ss//' with 'ss/'?

1 Answer 1

18
UPDATE sometable SET somefield=REPLACE(somefield,'/wordpress//','/wordpress/');

Edit

@Kevin asked me to explain this query, so here we go:

  • I assume the basic UPDATE is clear: In all rows of sometable assign a new value to somefield
  • the REPLACE() function does exactly what it says: It replaces text. In our use case we ant it to take the old value of somefield, then replace all ocurrencies of '/wordpress//' with '/wordpress/'
  • these two parts taken together mean, in all rows of sometable assign somefield the value, that results, if you replace all ocurrencies of '/wordpress//' with '/wordpress/' in the old value.
Sign up to request clarification or add additional context in comments.

3 Comments

Please add some explanation.
@Kevin I was quite sure, this is self-explaining! Anyway, I will add some explanation.
Great function and well explained!

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.