MySQL string function Locate and replace combat

Source: Internet
Author: User

For the locate function in MySQL, you can view the link: http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_locate

For the Replace function in MySQL, you can view the link: http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_replace

That is the two functions we are going to use today.

Use background:

Condition: The Body_value value of the Field_data_body table has such a regular string "/eapdomain/userfiles/1073/file/", "/eapdomain/userfiles/1073/file/" , where 1073 is the user ID. Don't be surprised why there is such a path!

Objective: To replace all the strings in Body_value with the "/sites/default/files/old/image/" and "/sites/default/files/old/file/".

Difficulty: The amount of data is slightly larger than 4,000 data, it is impossible to manually change, there may be more than one such path or not exist in each piece of data, the user ID is variable and uncertain.

Operate the database directly!

In Navicat, open your database command line tool (not limited to this tool), and enter the following command to replace the job.

Replace the string containing image

UPDATE field_data_body Set body_value = replace (body_value, substring (body_value, locate ('/eapdomain/userfiles/'), body _value), locate ('/image/', Body_value) +7-locate ('/eapdomain/userfiles/', body_value)), '/sites/default/files/old/ image/')

Replace the string containing file

UPDATE field_data_body Set body_value = replace (body_value, substring (body_value, locate ('/eapdomain/userfiles/'), body _value), locate ('/file/', Body_value) +6-locate ('/eapdomain/userfiles/', body_value)), '/sites/default/files/old/ file/')

There are many ways to achieve this, and you are welcome to leave a message below.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.