简体   繁体   中英

mysql find/replace string in fields

I have a table which has a few varchar fields named like this:

image_fullres | image_highres | image_mediumres | image_lowres | image_thumbnail

I currently have the same value for each of these fields(differing by row), so image_fullres has the same path as image_lowres and so on.

an example value for these paths would be http://mysite.com/images/image1.jpg

I would like to have each of these fields to contain a different and appropriate path name, so:

  • image_fullres has value of http://mysite.com/images/fullres/image1.jpg
  • image_highres has value of http://mysite.com/images/highres/image1.jpg
  • image_mediumres has value of http://mysite.com/images/mediumres/image1.jpg

and so on...

Since all of my data is already in the database with duplicate paths by row, what SQL can I execute that would change the value of each field's data to an appropriate path?

Example:

UPDATE table 
SET image_fullres = REPLACE(image_fullres, 'http://mysite.com/images/', 'http://mysite.com/images/fullres/')

This will update the image_fullres column for all rows in your table. You can do one such statement for each column, or combine them into one:

UPDATE table 
SET
image_fullres = REPLACE(image_fullres, 'http://mysite.com/images/', 'http://mysite.com/images/fullres/'),
image_highres = REPLACE(image_highres, 'http://mysite.com/images/', 'http://mysite.com/images/highres/'),
...
...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM