<p style="margin-top: 0.5em; margin-bottom: 0.9em; font-family: Georgia, serif, sans-serif, Arial, Verdana, Helvetica; font-size: 16px; line-height: 30px; ">在网站搬家、更换域名的时候,Drupal和Wordpress都是用的MySQL的数据库,很多配置信息是写在MySQL数据库里面的。我们需要将数据库里面的相关配置信息替换成新空间或域名的信息。这时我们就要用到MySQL的字符串替换函数Replace了。</p><p style="margin-top: 0.5em; margin-bottom: 0.9em; font-family: Georgia, serif, sans-serif, Arial, Verdana, Helvetica; font-size: 16px; line-height: 30px; ">MySQL Replace 替换函数语法:</p><p class="rteindent1" style="margin-top: 0.5em; margin-bottom: 0.9em; margin-left: 40px; font-family: Georgia, serif, sans-serif, Arial, Verdana, Helvetica; font-size: 16px; line-height: 30px; "><span class="geshifilter" style="display: inline; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; border-image: initial; "><code class="mysql geshifilter-mysql"><strong>update</strong> TABLE_NAME <strong>set</strong> FIELD_NAME <span style="color: rgb(204, 0, 153); ">=</span> <span style="color: rgb(0, 0, 153); ">replace</span><span style="color: rgb(255, 0, 255); ">(</span>FIELD_NAME<span style="color: rgb(0, 0, 51); ">,</span> <span style="color: rgb(0, 128, 0); ">"find this string"</span><span style="color: rgb(0, 0, 51); ">,</span> <span style="color: rgb(0, 128, 0); ">"replace found string with this string"</span><span style="color: rgb(255, 0, 255); ">)</span><span style="color: rgb(0, 0, 51); ">;</span></code></span></p><p style="margin-top: 0.5em; margin-bottom: 0.9em; font-family: Georgia, serif, sans-serif, Arial, Verdana, Helvetica; font-size: 16px; line-height: 30px; ">Replace 替换函数案例:</p><p class="rteindent1" style="margin-top: 0.5em; margin-bottom: 0.9em; margin-left: 40px; font-family: Georgia, serif, sans-serif, Arial, Verdana, Helvetica; font-size: 16px; line-height: 30px; "><span class="geshifilter" style="display: inline; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; border-image: initial; "><code class="mysql geshifilter-mysql"><strong>update</strong> client_table <strong>set</strong> url <span style="color: rgb(204, 0, 153); ">=</span> <span style="color: rgb(0, 0, 153); ">replace</span><span style="color: rgb(255, 0, 255); ">(</span>url<span style="color: rgb(0, 0, 51); ">,</span> <span style="color: rgb(0, 128, 0); ">"pythonclub.org"</span><span style="color: rgb(0, 0, 51); ">,</span><span style="color: rgb(0, 128, 0); ">"truevue.org"</span><span style="color: rgb(255, 0, 255); ">)</span></code></span></p><p style="margin-top: 0.5em; margin-bottom: 0.9em; font-family: Georgia, serif, sans-serif, Arial, Verdana, Helvetica; font-size: 16px; line-height: 30px; ">这样就能将MySQL中所有字符串替换成新的了。</p><p style="margin-top: 0.5em; margin-bottom: 0.9em; font-family: Georgia, serif, sans-serif, Arial, Verdana, Helvetica; font-size: 16px; line-height: 30px; "><br /></p><p style="margin-top: 0.5em; margin-bottom: 0.9em; font-family: Georgia, serif, sans-serif, Arial, Verdana, Helvetica; font-size: 16px; line-height: 30px; "><span style="font-family: simsun; font-size: 14px; line-height: 23px; text-align: left; ">REPLACE(str,from_str,to_str) </span><br style="font-family: simsun; font-size: 14px; line-height: 23px; text-align: left; " /><span style="font-family: simsun; font-size: 14px; line-height: 23px; text-align: left; ">将字符串 str 中的 from_str 替换成为 to_str </span><br style="font-family: simsun; font-size: 14px; line-height: 23px; text-align: left; " /><span style="font-family: simsun; font-size: 14px; line-height: 23px; text-align: left; ">mysql> SELECT REPLACE( 'www.mysql.com ', 'w ', 'Ww '); </span><br style="font-family: simsun; font-size: 14px; line-height: 23px; text-align: left; " /><span style="font-family: simsun; font-size: 14px; line-height: 23px; text-align: left; "> -> 'WwWwWw.mysql.com ' </span><br /></p>