欢迎来到日本小春精选,了解日本先从@日本生活基础课程开始!
欢迎 欢迎     登录 | 注册      消息
当前位置: 首页小组技术交流MySQL替换函数REPLACE替换字符串方法

[Linux]MySQL替换函数REPLACE替换字符串方法

<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>&nbsp;TABLE_NAME&nbsp;<strong>set</strong>&nbsp;FIELD_NAME&nbsp;<span style="color: rgb(204, 0, 153); ">=</span>&nbsp;<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>&nbsp;<span style="color: rgb(0, 128, 0); ">&quot;find this string&quot;</span><span style="color: rgb(0, 0, 51); ">,</span>&nbsp;<span style="color: rgb(0, 128, 0); ">&quot;replace found string with this string&quot;</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>&nbsp;client_table&nbsp;<strong>set</strong>&nbsp;url&nbsp;<span style="color: rgb(204, 0, 153); ">=</span>&nbsp;<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>&nbsp;<span style="color: rgb(0, 128, 0); ">&quot;pythonclub.org&quot;</span><span style="color: rgb(0, 0, 51); ">,</span><span style="color: rgb(0, 128, 0); ">&quot;truevue.org&quot;</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) &nbsp;&nbsp;</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; ">将字符串 &nbsp; str &nbsp; 中的 &nbsp; from_str &nbsp; 替换成为 &nbsp; to_str&nbsp;</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&gt; &nbsp; SELECT &nbsp; REPLACE( 'www.mysql.com ', &nbsp; 'w ', &nbsp; 'Ww ');&nbsp;</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; ">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; -&gt; &nbsp; 'WwWwWw.mysql.com '&nbsp;</span><br /></p>
2012-05-13 17:25:09 来自:小牛d
用户评论(0)
正序阅读
你的回应
登录 | 注册