ACTUALIZAR la base de datos mysql reemplazar cadenas

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

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

Gracias por adelantado


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="">domain</a></p> in which CONCAT() prepends the http:// before <p>

preguntado el 08 de noviembre de 11 a las 16:11

3 Respuestas

Intente agregar un 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

y su UPDATE la sintaxis sería:

UPDATE foo SET field=CONCAT('http://',field) WHERE LOCATE('http://', field)=0

respondido 08 nov., 11:20

+ 1, LOCATE is also another viable option. - Brad Christie

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

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.

respondido 08 nov., 11:20

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/','')

respondido 20 mar '13, 11:03

Porque 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… - Hibou57

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.