0

I have in my db strings like www.domain.com and http://www.domain.com. I want to prepend to all entries the http:// but not affect other urls and as a result have this: http://http://www.domain.com

Can this be done with mysql only? I have used REPLACE(field,'www','http://www'), but this replaces also the http://www with http://http://www

Thanks in advance

EDIT

I forgot to mention that in the field there might be entries which don't contain www or http://www and therefore I don't want to alter or maybe there are entries like <p><a href="www.domain.com/">domain</a></p> in which CONCAT() prepends the http:// before <p>

3 Answers 3

1

Try adding a WHERE clause to your update to only update fields that do not already have 'http://'. Test it out like this

 SELECT CONCAT('http://', field) FROM foo WHERE LOCATE('http://', field)=0

and your UPDATE syntax would be:

UPDATE foo SET field=CONCAT('http://',field) WHERE LOCATE('http://', field)=0
Sign up to request clarification or add additional context in comments.

1 Comment

+1, LOCATE is also another viable option.
0

I won't worry about performance as this seems like a one-off kind of script. That said, you can couple LEFT and CONCAT to achieve this:

UPDATE  mytable
SET     mycolumn = CONCAT('http://',mycolumn)
WHERE   LEFT(mycolumn,7) <> 'http://'

Do note that I'm not taking CapItaliZation in to account. You may also want to consider sanitizing the information either before adding it to the database, or maybe make a trigger to do it for you.

Comments

0

Search and Replace Query - mysql replace

Here is the SQL query to replace string in your MySQL database table:

UPDATE table_name SET column_name = REPLACE(column_name,'original_string','replace_string')

Here is what I did to change the path URLs in all my previous posts.

UPDATE `wp_posts` SET `post_content` = REPLACE(`post_content`,'http://localhost/','https://sureshkamal1.wordpress.com/')

1 Comment

Why no WHERE? Is this too much superfluous? Does it cost anything to add something like WHERE column_name LIKE CONCAT('%s', 'original_string', '%')? I wonder…

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.