Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
764 views
in Technique[技术] by (71.8m points)

mysql - Find and replace string with placeholders in SQL

Is it possible to search for a string like this:

http://getfile1.posterous.com/getfile/files.posterous.com/temp-2013-03-30/kBpHaviyoumcvCEHnGnrDfwHwmJIrnqrHrxhCnHwfBIHuDsxbemHlxwEwCdi/IMG_6870.jpg

while some parts are placeholders like:

http://getfile%.posterous.com/getfile/files.posterous.com/temp-2013-03-%/%/IMG_6870.jpg

and replace it with:

http://my.domain.com/wordpress/wp-content/uploads/2013/03/IMG_6870.jpg

Thank you for inputs!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

MySQL support for regular expressions is very limited, but you can try with a combination of SUBSTRING_INDEX and REPLACE string functions, like this:

UPDATE tablename
SET
  url =
  CONCAT(
    'http://my.domain.com/wordpress/wp-content/uploads/',
    REPLACE(
      REPLACE(
        SUBSTRING_INDEX(
          SUBSTRING_INDEX(url, '/', -3),
          '-',
          3),
        'temp-',
        ''),
      '-',
      '/'),
    '/',
    SUBSTRING_INDEX(url, '/', -1));

But you need to make sure that all strings have the same format. Please see fiddle here.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...